onCreate saveInstanceState Bundle 中的 FragmentActivity NullPointer
如果我将应用程序置于后台并在一段时间后恢复它,我会收到以下异常。
如果我更改方向或背景并立即恢复应用程序(onSaveInstanceState
和 onCreate
在两种情况下都执行),则 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Parcelable.Creator()
必须是static
像这样the
Parcelable.Creator<RunningLoadersList>()
must bestatic
like this