gpt4 book ai didi

android - 将序列化的接口(interface)对象从 Activity 解析为 fragment

转载 作者:行者123 更新时间:2023-11-29 23:17:57 25 4
gpt4 key购买 nike

我有接口(interface)Callback类如下所示,

public interface Callback extends Serializable {
void onResponse(int responseCode);
}

我使用上面的接口(interface)解析为参数回调对象,如下所示

private Callback callback = new Callback() {
@Override
public void onResponse(int responseCode) {
switch (responseCode) {
case ResponseCode.FACE_NOT_DETECT: {
notifyText.setText("Face not detected");
break;
}
case ResponseCode.FACE_TOO_FAR: {
notifyText.setText("Face too far, Please get closer to the camera");
break;
}
case ResponseCode.FACE_BLINK_TO_CAPTURE: {
notifyText.setText("Blink once to capture your face image");
break;
}
}
}
};

然后我用上面的callback反对发送到我的图书馆

FaceDetectFragment.newInstance(callback, mode);

在此FaceDetectFragment类,我如下操作参数,

 public static FaceDetectFragment newInstance(Callback callback, int mode) {
if (faceInstance != null) {
return faceInstance;
}
faceInstance = new FaceDetectFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(KEY_USER_CALLBACK, callback);
bundle.putInt(KEY_BIOMETRIC_MODE, mode);
faceInstance.setArguments(bundle);

return faceInstance;
}

然后我在 onCreate 中使用上面设置的参数方法如下图,

if (getArguments() != null) {
clientCallback = (Callback) getArguments().getSerializable(KEY_USER_CALLBACK);
biometricMode = getArguments().getInt(KEY_BIOMETRIC_MODE);
}

然后在这里我将多次使用回调对象,如下所示,

clientCallback.onResponse(ResponseCode.FACE_NOT_DETECT);

所有功能都正常,但是当我暂停应用程序时,会抛出 RuntimeException,异常如下所示,

 Process: com.ej.face_demo_android, PID: 29679
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.ej.face_demo_android.FaceDetectActivity$2)
at android.os.Parcel.writeSerializable(Parcel.java:1536)
at android.os.Parcel.writeValue(Parcel.java:1484)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:733)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1133)
at android.os.Parcel.writeBundle(Parcel.java:773)
at android.support.v4.app.FragmentState.writeToParcel(FragmentState.java:124)
at android.os.Parcel.writeTypedArray(Parcel.java:1316)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:639)
at android.os.Parcel.writeParcelable(Parcel.java:1505)
at android.os.Parcel.writeValue(Parcel.java:1411)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:733)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1133)
at android.os.Parcel.writeBundle(Parcel.java:773)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:4146)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4148)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.io.NotSerializableException: com.ej.face_demo_android.FaceDetectActivity
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at android.os.Parcel.writeSerializable(Parcel.java:1531)
at android.os.Parcel.writeValue(Parcel.java:1484) 
at android.os.Parcel.writeArrayMapInternal(Parcel.java:733) 
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408) 
at android.os.Bundle.writeToParcel(Bundle.java:1133) 
at android.os.Parcel.writeBundle(Parcel.java:773) 
at android.support.v4.app.FragmentState.writeToParcel(FragmentState.java:124) 
at android.os.Parcel.writeTypedArray(Parcel.java:1316) 
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:639) 
at android.os.Parcel.writeParcelable(Parcel.java:1505) 
at android.os.Parcel.writeValue(Parcel.java:1411) 
at android.os.Parcel.writeArrayMapInternal(Parcel.java:733) 
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408) 
at android.os.Bundle.writeToParcel(Bundle.java:1133) 
at android.os.Parcel.writeBundle(Parcel.java:773) 
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:4146) 
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4148) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

据我所知,当我暂停应用程序时,尝试保存应用程序当前状态的应用程序无法识别序列化回调以保存在当前状态。我应该怎么做才能处理这个异常?

更新:当我们考虑 fragment 生命周期时,onPause state 被触发,当前状态 wi 保存在堆栈中,然后我的序列化对象将通过 main thread 再次包装到 bundle 中.这是我分析时出现的时间错误

提前感谢您的宝贵时间!

最佳答案

要回答你的问题,“真正的问题”是你试图序列化一个类的实例,而不是一个接口(interface)。您的类是一个名为 com.ej.face_demo_android.FaceDetectActivity$2 的匿名内部类,您在此处创建:

private Callback callback = new Callback() {
@Override
public void onResponse(int responseCode) {
switch (responseCode) {
case ResponseCode.FACE_NOT_DETECT: {
notifyText.setText("Face not detected");
break;
}
case ResponseCode.FACE_TOO_FAR: {
notifyText.setText("Face too far, Please get closer to the camera");
break;
}
case ResponseCode.FACE_BLINK_TO_CAPTURE: {
notifyText.setText("Blink once to capture your face image");
break;
}
}
}
};

不可能序列化这个匿名内部类。参见 NotSerializableException on anonymous class了解更多细节。

如果您确实需要这样做,请创建一个真正的独立类而不是匿名内部类。你可能会更幸运。

关于android - 将序列化的接口(interface)对象从 Activity 解析为 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54978960/

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