onCreate saveInstanceState Bundle 中的 FragmentActivity NullPointer

发布于 2025-01-02 20:46:01 字数 4394 浏览 2 评论 0原文

如果我将应用程序置于后台并在一段时间后恢复它,我会收到以下异常。
如果我更改方向或背景并立即恢复应用程序(onSaveInstanceStateonCreate 在两种情况下都执行),则 savingInstanceState Bundle 包含正确的 ArrayList 并且一切正常。

02-05 14:42:06.254 E/AndroidRuntime(24355): java.lang.RuntimeException:无法启动活动 ComponentInfo{com.savant.donordetailsviewpagertitle/com.savant.donordetailsviewpagertitle.activities.DonorDetailsContainerFragmentActivity}: java.lang.NullPointerException:预期的接收者类型 com.savant.donordetailsviewpagertitle.activities.DonorDetailsContainerFragmentActivity$RunningLoadersList, 但得到空

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2025-01-09 20:46:01

Parcelable.Creator() 必须是 static 像这样

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

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

public static final Parcelable.Creator<RunningLoadersList> CREATOR = 
new Parcelable.Creator<RunningLoadersList>() {
...
...
.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文