【问题标题】:Unfortunately app has stopped while debugging不幸的是,应用程序在调试时停止了
【发布时间】:2015-11-25 14:06:52
【问题描述】:

我在我的速度计应用上调试应用时收到此消息。

这是我的 java 类:

public class MainActivity extends AppCompatActivity implements LocationListener {

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @TargetApi(Build.VERSION_CODES.M)

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

        LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    public void requestPermissions(@NonNull String[] permissions, int requestCode)
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            return;
        }
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        this.onLocationChanged(null);

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onLocationChanged(Location location) {
        TextView txt = (TextView) this.findViewById(R.id.textView);

        if (location == null) {
            txt.setText("-.- m/s");
        } else {
            float nCurrentSpeed = location.getSpeed();
            txt.setText(nCurrentSpeed + "m/s");
        }

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://mycompany.com.speedometer3/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://mycompany.com.speedometer3/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

这是我的 LOGCAT

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoSuchMethodError: mycompany.com.speedometer3.MainActivity.checkSelfPermission
      at mycompany.com.speedometer3.MainActivity.onCreate(MainActivity.java:39)
      at android.app.Activity.performCreate(Activity.java:5255)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
      at android.app.ActivityThread.access$700(ActivityThread.java:154)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:5306)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
      at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • java.lang.NoSuchMethodError: mycompany.com.speedometer3.MainActivity.checkSelfPermission 发布该代码
  • NoSuchMethodError: ... checkSelfPermission 您可能在 Android 6.0 之前的设备上运行它。在使用该功能之前,您必须检查build.version_codes.marshmallow

标签: java android android-layout android-intent


【解决方案1】:

您在 Android 6.0 之前的设备上运行您的应用。在 Android 6.0 中添加了checkSelfPermission()

您的选择是:

  • 将项目的 minSdkVersion 设置为 23,因此它只能在 Android 6.0+ 设备上运行

  • 使用ContextCompat.checkSelfPermission() 而不是checkSelfPermission()

  • 仅在 Android 6.0+ 设备上调用 checkSelfPermission(),通过比较 Build.VERSION.SDK_INTBuild.VERSION_CODES.M

【讨论】:

    【解决方案2】:

    checkSelfPermission(String permission) 方法在您运行应用程序的设备上不可用。此方法是在 Android 6.0 中引入的,而您的设备的版本低于该版本。

    你可以这样修复它:

    if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    public void requestPermissions(@NonNull String[] permissions, int requestCode)
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            return;
        }
    }
    

    由于您的 minSdkVersion 可能低于 23,请将其放在单独的函数中,如下所示:

    @TargetApi(23)
    private boolean checkPermissionLocation() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    public void requestPermissions(@NonNull String[] permissions, int requestCode)
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for Activity#requestPermissions for more details.
                return false;
            }
        }
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 2016-10-03
      • 2018-03-23
      • 2015-05-09
      • 2016-11-19
      相关资源
      最近更新 更多