NullPointerException on onSaveInstanceState with A

2019-01-19 03:26发布

问题:

I am receiving a NullPointerException when I LEAVE a FragmentActivity and go to a ListActivity. I actually start to see the list before I get the Force Close. But the LogCat refers to the Activity that I just came from.

The LogCat says this:

08-31 07:38:53.356: E/AndroidRuntime(8134): FATAL EXCEPTION: main
08-31 07:38:53.356: E/AndroidRuntime(8134): java.lang.NullPointerException
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at com.---.myApp.MasterCat.onSaveInstanceState(MasterCat.java:119)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.Activity.performSaveInstanceState(Activity.java:1137)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3077)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3136)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread.access$900(ActivityThread.java:142)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1235)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.os.Looper.loop(Looper.java:137)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at android.app.ActivityThread.main(ActivityThread.java:4928)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at java.lang.reflect.Method.invokeNative(Native Method)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at java.lang.reflect.Method.invoke(Method.java:511)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
08-31 07:38:53.356: E/AndroidRuntime(8134):     at dalvik.system.NativeStart.main(Native Method)

Line 119 in MasterCat is:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
}

I am not sure what other code to show, please let me know. My basic setup contains 4 static inner classes that are all fragments inside the parent FragmentActivity (using the support library). It uses tabs and a viewpager.

EDIT, More Code:

public class MasterCat extends FragmentActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_layout);
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager
                .setOnPageChangeListener(
                    new ViewPager.SimpleOnPageChangeListener() {

                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {}

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {}

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
            case 0:
            {
                f = new MasterFrag();
                Bundle args = new Bundle();
                f.setArguments(args);
                break;
            }
            case 1:
            {
                f = new FeaturedFrag();
                Bundle args = new Bundle();
                f.setArguments(args);
                break;
            }
            case 2:
            {
                f = new TopFrag();
                Bundle args = new Bundle();
                f.setArguments(args);
                break;
            }
            case 3:
            {
                f = new NewFrag();
                Bundle args = new Bundle();
                f.setArguments(args);
                break;
            }
            default:
                throw new IllegalArgumentException("not this many fragments: "
                    + position);
            }
            return f;
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return getString(R.string.mastercattab1).toUpperCase();
            case 1:
                return getString(R.string.mastercattab2).toUpperCase();
            case 2:
                return getString(R.string.mastercattab3).toUpperCase();
            case 3:
                return getString(R.string.mastercattab4).toUpperCase();
            }
            return null;
        }
    }
}

A Typical Fragment has an outline like this:

public static class FeaturedFrag extends Fragment {

    public FeaturedFrag() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.rateithome, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}

回答1:

It looks like this is a known bug in the Android Support Library. Fortunately, it's been patched.

To fix the bug:

  1. Use the SDK Manager to update to the latest version of the support library
  2. Replace the android-support-v4.jar file in your application's libs/ folder with the new version that you just downloaded


回答2:

Try this:

public void onSaveInstanceState(Bundle savedInstanceState) {

if (savedInstanceState != null) 
   {
      super.onSaveInstanceState(savedInstanceState);
    }
 }


回答3:

If you just want a quick workaround, add this to your Fragment code:

@Override
public void onSaveInstanceState(Bundle outState) {
    // Workaround to avoid NPE from support library bug:
    setUserVisibleHint(false);

    super.onSaveInstanceState(outState);
}

The setUserVisibleHint property is indeed just a hint for optimization and shouldn't cause you any trouble, see Android reference documentation.

Of course, updating the Support library as answered by theisenp might be more desirable in the long run.



回答4:

I think you have used this style AppTheme.NoActionBar

in manifest file and in java class ,you are using actionbar that's why it cause crashes.

Can you show me your manifest file and style.xml