尝试拨打给定号码时应用程序突然退出

App Suddenly Exits When Tried to Call given number

我尝试使用 Intent 创建来电应用程序,在 android studio ide 代码没有错误,但是当它在实时设备上 运行 并在 EditText 中输入号码时 &按下呼叫按钮应用程序突然退出。我无法找到导致此应用程序行为的原因以及为什么无法拨打电话。我如何让它工作?

MainActivity.java

    public class MainActivity extends AppCompatActivity {
 EditText txt_phn;
 Button btn_call;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt_phn=(EditText)findViewById(R.id.txt_phn);
        btn_call=(Button)findViewById(R.id.btn_call);
        btn_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                mck();
            }
        });


    }
    private void mck()
    {


        
        String number = "tel:" + txt_phn.getText().toString().trim();

        Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));

        try {
            startActivity(callIntent);
            finish();
            Log.i("Finished making a call", "");
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "Call faild, please try again later.", Toast.LENGTH_SHORT).show();
        }


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
   
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.caller">
    <uses-permission android:name="android.permission.CALL_PHONE"/>




    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/txt_phn"
        android:layout_width="379dp"
        android:layout_height="70dp"
        android:layout_marginTop="148dp"
        android:ems="10"
        android:hint="Please Enter Phone Number To Call"
        android:inputType="text"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="16dp" />

    <Button
        android:id="@+id/btn_call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="360dp"
        android:text="Call"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2020-08-10 20:04:47.337 23806-23806/com.example.caller E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.caller, PID: 23806
    java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{70113a3 23806:com.example.caller/u0a425} (pid=23806, uid=10425) with revoked permission android.permission.CALL_PHONE
        at android.os.Parcel.createException(Parcel.java:2071)
        at android.os.Parcel.readException(Parcel.java:2039)
        at android.os.Parcel.readException(Parcel.java:1987)
        at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3851)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1714)
        at android.app.Activity.startActivityForResult(Activity.java:5210)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:5168)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at android.app.Activity.startActivity(Activity.java:5539)
        at android.app.Activity.startActivity(Activity.java:5507)
        at com.example.caller.MainActivity.mck(MainActivity.java:49)
        at com.example.caller.MainActivity.access[=13=]0(MainActivity.java:19)
        at com.example.caller.MainActivity.onClick(MainActivity.java:33)
        at android.view.View.performClick(View.java:7260)
        at android.view.View.performClickInternal(View.java:7237)
        at android.view.View.access00(View.java:802)
        at android.view.View$PerformClick.run(View.java:27915)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:915)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.wm.ActivityStackSupervisor.checkStartAnyActivityPermission(ActivityStackSupervisor.java:1058)
        at com.android.server.wm.ActivityStarter.startActivity(ActivityStarter.java:762)
        at com.android.server.wm.ActivityStarter.startActivity(ActivityStarter.java:585)
        at com.android.server.wm.ActivityStarter.startActivityMayWait(ActivityStarter.java:1298)
        at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:516)

所以我终于得到了修复..感谢“David Wasser”,因为我最初没有看 logcat。通过他的建议,我查看了 logcat 并发现我没有在代码中添加的运行时调用权限错误,这个提示也由“vikas kumar”给出,感谢他.. 现在的变化。以下是我在 mainactivity 中所做的更改:-

  if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            startActivity(callIntent);
        } else {
            requestPermissions(new String[]{CALL_PHONE}, 1);
        }

这需要电话的运行时权限,一旦获得许可,它将直接拨打电话。

Why This changes needed ?
Because from Android 6.0 Above there were api changes & also permission related changes.
& i followed older example of calling app tutorial.