【问题标题】:No Activity found to handle Intent { act=android.settings.LOCALE_SETTINGS }未找到处理 Intent { act=android.settings.LOCALE_SETTINGS } 的 Activity
【发布时间】:2019-10-10 02:49:22
【问题描述】:

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

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

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

    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
    

    手机/设备可能完全缺少区域设置屏幕,这会导致崩溃。

    【讨论】:

      【解决方案2】:

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

      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 as -

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

      【讨论】:

        【解决方案3】:
        Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //This line solve my issue
        startActivity(viewIntent);
        

        【讨论】:

          猜你喜欢
          • 2012-02-20
          • 2021-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多