FragmentActivity NullPointer in onCreate savedInstanceState Bundle

孤者浪人 提交于 2019-11-28 02:05:51

问题


If I background the app and resume it after a period of time I receive the following exception.
If I change orientation or background and resume the app straight away (the onSaveInstanceState and onCreate is executed on both occasions) then the savedInstanceState Bundle contains the correct ArrayList and everything works fine.

02-05 14:42:06.254 E/AndroidRuntime(24355): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.savant.donordetailsviewpagertitle/com.savant.donordetailsviewpagertitle.activities.DonorDetailsContainerFragmentActivity}: java.lang.NullPointerException: expected receiver of type com.savant.donordetailsviewpagertitle.activities.DonorDetailsContainerFragmentActivity$RunningLoadersList, but got null

    private class RunningLoadersList extends ArrayList<RunningLoader> implements
        Parcelable {

    private static final long   serialVersionUID    = 663585476779879096L;

    public RunningLoadersList() {
    }

    @SuppressWarnings("unused")
    public RunningLoadersList(Parcel in) {
        this();
        readFromParcel(in);
    }

    private void readFromParcel(Parcel in) {

        this.clear();

        // First we have to read the list size
        int size = in.readInt();

        for (int i = 0; i < size; i++) {
            RunningLoader r = new RunningLoader(in.readInt(),
                    in.readBundle());
            this.add(r);
        }
    }

    public int describeContents() {
        return 0;
    }

    public final Parcelable.Creator<RunningLoadersList> CREATOR = new Parcelable.Creator<RunningLoadersList>() {
                                                                    public RunningLoadersList createFromParcel(Parcel in) {
                                                                        return new RunningLoadersList(in);
                                                                    }

                                                                    public RunningLoadersList[] newArray(int size) {
                                                                        return new RunningLoadersList[size];
                                                                    }
                                                                };

    public void writeToParcel(Parcel dest, int flags) {
        int size = this.size();

        // We have to write the list size, we need him recreating the list
        dest.writeInt(size);

        for (int i = 0; i < size; i++) {
            RunningLoader r = this.get(i);

            dest.writeInt(r.id);
            dest.writeBundle(r.args);
        }
    }
}

static final class RunningLoader {
    private final int       id;
    private final Bundle    args;

    RunningLoader(int _id, Bundle _args) {
        id = _id;
        args = _args;
    }
}



    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This has to be called before setContentView and you must use the
    // class in android.support.v4.view and NOT android.view
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.main);

    Log.d(LOG_TAG, "onCreate");

    mAdapter = new ViewPagerTitleAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.tabindicator);
    indicator.setViewPager(mPager);

    // first check if we already have a running loader
    if ((savedInstanceState != null)
            && savedInstanceState.containsKey("RUNNING_LOADERS")) {
        mRunningLoaders = savedInstanceState.getParcelable("RUNNING_LOADERS");
    }

    if (mRunningLoaders == null) {
        mRunningLoaders = new RunningLoadersList();
    }

    if (mRunningLoaders != null) {
        for (int i = 0; i < mRunningLoaders.size(); i++) {
            StartLoader(mRunningLoaders.get(i).id,
                    mRunningLoaders.get(i).args);
        }
    }

    if (getSupportLoaderManager().hasRunningLoaders()) {
        setProgressBarIndeterminateVisibility(Boolean.TRUE);
    } else {
        setProgressBarIndeterminateVisibility(Boolean.FALSE);
    }
}


    @Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable("RUNNING_LOADERS", mRunningLoaders);

    Log.d(LOG_TAG, "onSaveInstanceState");
}

回答1:


the Parcelable.Creator<RunningLoadersList>() must be static like this

public static final Parcelable.Creator<RunningLoadersList> CREATOR = 
new Parcelable.Creator<RunningLoadersList>() {
...
...
.
}


来源:https://stackoverflow.com/questions/9151755/fragmentactivity-nullpointer-in-oncreate-savedinstancestate-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!