未找到处理Intent { act=android.settings.LOCALE_SETTINGS }的Activity

2fjabf4q  于 2023-08-01  发布在  Android
关注(0)|答案(3)|浏览(83)

我正在创建一个检查当前位置的代码,但是,当我检查当前位置时,它崩溃了。我查了一下Stack Trace发现了这个。我读过其他论坛,但我不熟悉他们的解决方案。我也是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();
}

pxyaymoc

pxyaymoc1#

始终使用此方法启动应用程序以外的Intent-

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
    }

w8f9ii69

w8f9ii692#

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

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

字符串
手机/设备可能完全缺少区域设置屏幕,从而导致崩溃。

iyr7buue

iyr7buue3#

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

字符串

相关问题