gpt4 book ai didi

android - 使用隐式 Intent 时如何在我的代码中解析 A​​ctivityNotFoundException?

转载 作者:行者123 更新时间:2023-11-29 19:11:31 26 4
gpt4 key购买 nike

主 Activity .java

public class MainActivity extends AppCompatActivity {


EditText edit;
Button btn;
boolean b = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edit = (EditText) findViewById(R.id.phone);
btn = (Button) findViewById(R.id.call);

permissionRequest();


btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (b == false)
{

permissionRequest();
}

else
{

if (edit.getText().toString().equals(" "))
{
Toast.makeText(MainActivity.this, "Enter the number first", Toast.LENGTH_LONG).show();
}
else
{
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse(edit.getText().toString()));
startActivity(i); //This line is coming in red
}
}
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
b = true;
} else {

b = false;
}
}


}

void permissionRequest() {

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
}
}
}

list 文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ankit.dialer">
<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>

在这里,我正在创建一个应用程序来调用电话。问题是,当我启动应用程序时,它会请求许可,但在我提供许可后,单击按钮时它会显示 ActivityNotFoundException 说“找不到处理 Intent 的 Activity ”。如何解决这个问题。

我的 startActivity(i) 也有红色下划线,表示“调用需要权限,这可能会被用户拒绝”,但程序正在编译和执行,但当程序在按钮上崩溃时,单击然后在 android 中监视它显示由该行引起的异常。

最佳答案

按照以下方式更改您的代码

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:" + edit.getText().toString()));
startActivity(i); //This line is coming in red

if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED)
{

/**********************
*
* WRITE UR CODE INSIDE THIS BLOCK TO HANDLE SITUATION WHEN
THE APP IS NOT GRANTED " ACTION_CALL" PERMISSION
* /

return;
}
}

关于android - 使用隐式 Intent 时如何在我的代码中解析 A​​ctivityNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45129781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com