0

I have a problem with sample code from google maps ! This code worked when i used eclipse. I changed for android studio and i have a wird error.

Error is : ( i have no onSaveInstanceState method)

Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
            at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
            at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
            at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
            at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
            at android.support.v4.app.DialogFragment.show(DialogFragment.java:138)
            at com.example.smartoo.HomeActivity.servicesConnected(HomeActivity.java:325)
            at com.example.smartoo.HomeActivity.stopUpdates(HomeActivity.java:373)
            at com.example.smartoo.HomeActivity.onStop(HomeActivity.java:209)
            at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1212)
            at android.app.Activity.performStop(Activity.java:5376)
            at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3185)
            at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3234)
            at android.app.ActivityThread.access$1100(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            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)

Error is on this line :

errorFragment.show(getSupportFragmentManager(), LocationUtils.APPTAG);

The code :

/**
     * Verify that Google Play services is available before making a request.
     *
     * @return true if Google Play services is available, otherwise false
     */
    private boolean servicesConnected() {

        // Check that Google Play services is available
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        // If Google Play services is available
        if (ConnectionResult.SUCCESS == resultCode) {
            // In debug mode, log the status
            Log.d(LocationUtils.APPTAG, getString(R.string.play_services_available));

            // Continue
            return true;
        // Google Play services was not available for some reason
        } else {
            // Display an error dialog
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0);
            if (dialog != null) {
                ErrorDialogFragment errorFragment = new ErrorDialogFragment();
                errorFragment.setDialog(dialog);
                errorFragment.show(getSupportFragmentManager(), LocationUtils.APPTAG);
            }
            return false;
        }
    }

Thx !

Maxouille
  • 2,729
  • 2
  • 19
  • 42

1 Answers1

1

Normally, the above code is called inside onStart/OnResume method or after these methods, not inside onStop/onSaveInstance. You are trying to do some operation after activity is getting killed. Perform serivcesConnected call before onStop/finish/saveInstances method call.

Sreejith B Naick
  • 1,203
  • 7
  • 13