NullPointerException android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState

时间:2013-01-25 06:51:40

标签: android android-intent android-fragments

使用Share Intent应用程序崩溃时出现以下错误

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

2 个答案:

答案 0 :(得分:11)

它是一种各种各样的错误(或许不是)。但请尝试以下操作:对于所有片段,覆盖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);
}

link有更多见解

答案 1 :(得分:5)

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