NullPointerException异常android.support.v4.app.Fragm

2019-08-08 05:25发布

在使用共享意向的应用程序崩溃并出现以下错误

01-25 12:16:09.270: E/AndroidRuntime(28699): FATAL EXCEPTION: main
01-25 12:16:09.270: E/AndroidRuntime(28699): java.lang.NullPointerException
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:127)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.app.Activity.performSaveInstanceState(Activity.java:1113)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1188)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2805)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:2863)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.app.ActivityThread.access$900(ActivityThread.java:127)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1173)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.os.Looper.loop(Looper.java:137)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at android.app.ActivityThread.main(ActivityThread.java:4507)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at java.lang.reflect.Method.invoke(Method.java:511)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-25 12:16:09.270: E/AndroidRuntime(28699):    at dalvik.system.NativeStart.main(Native Method)

PlacesActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_share:
        // Google Analytics Tracking
        GATracker.sendEvent("ui_action", "button_press", "share",
                click_value);

        shareIt();
        break;
    default:
        break;
    }
    return true;
}


private void shareIt() {
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");

    String shareText = name + "\n" + distance + " kms - " + days + " days"
            + "\n" + todo + "\n\n"
            + "Check out Nomad for Android http://goo.gl/i1mH9";

    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "Check out this!!");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareText);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

Answer 1:

它的一个种类(或者也许不是)的bug。 但尝试folloing:对于所有的片段,覆盖下面的onSaveInstanceState的

@Override 
public void onSaveInstanceState(Bundle outState) 
{
//first saving my state, so the bundle wont be empty.
outState.putString("WORKAROUND_FOR_BUG_19917_KEY",  "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}

这个环节有更深入的了解



Answer 2:

尝试使用,而不是FragmentPageAdapter FragmentStatePagerAdapter。 是一个更好的办法。



文章来源: NullPointerException android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState