未找到处理意向 { act=android.settings.LOCALE_SETTINGS } 的活动



我正在创建一个检查当前位置的代码,但是,当我检查当前位置时,它崩溃了。我查看了堆栈跟踪并看到了这个。我阅读了其他论坛,但我不熟悉他们的解决方案。我也是Java的初学者。我使用亚马逊Fire平板电脑下载了这个应用程序。当我尝试在模拟器上测试它时,该应用程序没有崩溃。

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.LOCALE_SETTINGS }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1797)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
at android.app.Activity.startActivityForResult(Activity.java:3761)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:3722)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:4032)
at android.app.Activity.startActivity(Activity.java:4000)
at com.example.calculatorandlocationfinderfinal.MainActivity$7.onClick(MainActivity.java:181)
at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

下面是它发生的代码。

private void onGPS() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_LOCALE_SETTINGS));
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}

始终使用此方法启动应用以外的意图 -

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
if(intent.resolveActivity(context.getPackageManager()) != null){
startActivity(intent);
}else{
//handle activity not found
}

这样你就不会得到ActivityNotFoundException。

您也可以将 try catch 用作 -

try {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
} catch (e: ActivityNotFoundException) {
//handle activity not found
}

我想你想导航到GPS设置屏幕。 试试这个。

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

电话/设备可能完全缺少区域设置屏幕,从而导致崩溃。

Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //This line solve my issue
startActivity(i);

最新更新