gpt4 book ai didi

android - 保存 fragment 状态时出现 NotSerializableException

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:31 26 4
gpt4 key购买 nike

我偶尔会看到我并不真正理解的异常。这就是我所做的:我有一个界面

public interface IResultProcessor<T> extends Serializable {

void processResult(T result);

}

,我在这个类中使用的实例:

public class ConfirmationDialogFragment extends DialogFragment {

public static void executeAfterConfirmation(FragmentActivity activity, IResultProcessor<Void> runnable,
String title, String message, int icon, String positiveButtonText, String negativeButtonText) {

ConfirmationDialogFragment fragment = new ConfirmationDialogFragment();
Bundle args = new Bundle();
args.putSerializable(ARG_RUNNABLE, runnable);
fragment.setArguments(args);
fragment.show(activity.getSupportFragmentManager(), "dialog");
}

}

onCreateDialog() 方法中,我收到 IProcessResult 实例,如果用户确认则执行它。

然后我在 Activity 中使用所有这些,如下所示:

@SuppressWarnings("serial") 
IResultProcessor<Void> processor = new IResultProcessor<Void>() {

@Override
public void processResult(Void result) {
new AsyncTask<Void, Void, String>() {

@Override
protected String doInBackground(Void... params) {
DBFacade.INSTANCE.saveSession(session);
return "My msg...";
}

@Override
protected void onPostExecute(String result) {
Toast.makeText(MyActivity.this, result, Toast.LENGTH_LONG).show();
}

}.execute((Void) null);
}
};
ConfirmationDialogFragment.executeAfterConfirmation(this, processor, "Save session?", "dialog text", R.drawable.ic_dialog_info, "Save", "Don't save");

AsyncTask中保存的session对象没有实现Serializable(但是Parcelable因为其他原因)并且是 MyActivity 的一个字段。

这是我看到的异常:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = MyActivity$3)
at android.os.Parcel.writeSerializable(Parcel.java:1279)
at android.os.Parcel.writeValue(Parcel.java:1233)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1619)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:132)
at android.os.Parcel.writeTypedArray(Parcel.java:1102)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:357)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1619)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2078)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:2874)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.NotSerializableException: MyActivity
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at android.os.Parcel.writeSerializable(Parcel.java:1274)
... 23 more
java.io.NotSerializableException: MyActivity
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at android.os.Parcel.writeSerializable(Parcel.java:1274)
at android.os.Parcel.writeValue(Parcel.java:1233)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1619)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:132)
at android.os.Parcel.writeTypedArray(Parcel.java:1102)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:357)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1619)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2078)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:2874)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

知道这里发生了什么吗?

最佳答案

我还没有完全理解,但显然解决了这个问题。当 Activity 被销毁时抛出异常,因为另一个 Activity 进入前台。我通过将匿名 IResultProcessor 类转换为我的 Activity 的静态内部类来解决它。

关于android - 保存 fragment 状态时出现 NotSerializableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13156727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com