简体   繁体   中英

FATAL EXCEPTION: main on Android App

I'm getting this fatal exception: main error. It is only when I click on the 'Pick A Place' button on my app.

The logcat report when clicking on the 'Pick A Place' button is below:

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)

The logcat points to a 'NoSuchMethodError' when checking for 'self permission' to do with the method 'calculateCurrentCooridnates'. This method is below:

@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);
}

The ToDo was automatically generated when Android Studio automatically added the '@TargetAPI' bit. With this in mind I added the following to the manifest and as I got no more errors I thought this would be enough:

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

However, the app just crashes.

Any help greatly appreciated.

Thanks

扩展AppCompatActivity将解决此问题。

Do like this

public class SettingsActivity extends AppCompatActivity {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM