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

5

我正在创建一段检查当前位置的代码,但是在检查当前位置时,它崩溃了。我查看了堆栈跟踪并看到了这个。我阅读了其他论坛,但我对他们的解决方案不熟悉。我也是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();
}
3个回答

1
始终使用此方法启动除您的应用之外的意图 -
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
    }

0

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

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

手机/设备可能完全缺少语言环境设置屏幕,这会导致崩溃。


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

回答需要支持信息 您的回答可以通过提供更多的支持信息来改进。请[编辑]以添加进一步的细节,例如引用或文档,以便他人可以确认您的回答是否正确。您可以在帮助中心找到关于如何撰写良好回答的更多信息。 - moken

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接