[英]FragmentActivity NullPointer in onCreate savedInstanceState Bundle
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. 如果我改变方向或背景并立即恢复应用程序(在两种情况下都执行
onSaveInstanceState
和onCreate
),那么savedInstanceState Bundle包含正确的ArrayList,一切正常。
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
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,但得到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");
}
the Parcelable.Creator<RunningLoadersList>()
must be static
like this Parcelable.Creator<RunningLoadersList>()
必须是这样的static
public static final Parcelable.Creator<RunningLoadersList> CREATOR =
new Parcelable.Creator<RunningLoadersList>() {
...
...
.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.