致命异议:主要在Android App上

时间:2017-04-16 11:06:22

标签: java android main fatal-error android-gps

我得到了这个致命的例外:主要错误。只有当我点击我的应用程序上的“选择一个地方”按钮时才会这样做。

点击“选择地点”按钮时的logcat报告如下:

04-13 13:52:19.418 10737-10737/cct.mad.lab E/AndroidRuntime: FATAL EXCEPTION: main
                                                         Process: cct.mad.lab, PID: 10737
                                                         java.lang.NoSuchMethodError: cct.mad.lab.SettingsActivity.checkSelfPermission
                                                             at cct.mad.lab.SettingsActivity.calculateCurrentCoordinates(SettingsActivity.java:679)
                                                             at cct.mad.lab.SettingsActivity.onPrepareDialog(SettingsActivity.java:581)
                                                             at android.app.Activity.onPrepareDialog(Activity.java:3061)
                                                             at android.app.Activity.showDialog(Activity.java:3124)
                                                             at android.app.Activity.showDialog(Activity.java:3075)
                                                             at cct.mad.lab.SettingsActivity$8.onClick(SettingsActivity.java:374)
                                                             at android.view.View.performClick(View.java:4438)
                                                             at android.view.View$PerformClick.run(View.java:18422)
                                                             at android.os.Handler.handleCallback(Handler.java:733)
                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                             at android.os.Looper.loop(Looper.java:136)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                             at dalvik.system.NativeStart.main(Native Method)

当检查“自我许可”时,logcat指向'NoSuchMethodError'来处理'calculateCurrentCooridnates'方法。这种方法如下:

@TargetApi(Build.VERSION_CODES.M)
private void calculateCurrentCoordinates() {
    float lat = 0, lon = 0;

    try {
        LocationManager locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    Activity#requestPermissions
            // 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;
        }
        Location recentLoc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        lat = (float) recentLoc.getLatitude();
        lon = (float) recentLoc.getLongitude();
    } catch (Exception e) {
        Log.e(DEBUG_TAG, "Location failed", e);
    }

    mFavPlaceCoords = new GPSCoords(lat, lon);
}

当Android Studio自动添加“@TargetAPI”位​​时,会自动生成ToDo。考虑到这一点,我在清单中添加了以下内容,因为我没有更多错误,我认为这就足够了:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

但是,应用程序崩溃了。

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:0)

扩展AppCompatActivity将解决问题。

答案 1 :(得分:0)

这样做

公共类SettingsActivity扩展了AppCompatActivity {