gpt4 book ai didi

android - 将接口(interface)回调添加到 Bundle 后出现 BadParcelableException(非常罕见且随机发生)?

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

我在尝试从 Bundle 中检索时看到(不经常 - 很少 = cca 1/1000 次)BadParcelableException

有人知道为什么会这样吗?我认为它应该总是发生,或者永远不会发生,而不是随机发生。这是我的代码:

  1. 我正在放入 Bundle 中的 Parcelable 对象:

    public interface ButtonPressedCallback extends Parcelable {
    public void onButtonPressed ();
    }
  2. 这就是我把它放在 Bundle 中的方式

    bundle.putString(MyFragment.TITLE, title);
    bundle.putParcelable(MyFragment.CALLBACK , callback);
  3. 这就是我检索它的方式(这里它崩溃了)

    public class MyFragment extends Fragment {    
    private String title;
    private ButtonPressedCallback callback;

    public static final String TITLE = "TITLE";
    public static final String CALLBACK = "CALLBACK";

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle b = getArguments();

    title = getArguments().getString(TITLE); // it crashes here
    callback = b.getParcelable(CALLBACK );
    ...
    }

它在检索普通字符串 TITLE 时神秘地在线崩溃(奇怪....),但我 100% 确定那条线没问题,它实际上在下面崩溃了——因为它是在我将接口(interface)添加到 Bundle 时开始的

这里是堆栈跟踪:

Caused by: android.os.BadParcelableException: 
at android.os.Parcel.readParcelableCreator (Parcel.java:2875)
at android.os.Parcel.readParcelable (Parcel.java:2797)
at android.os.Parcel.readValue (Parcel.java:2700)
at android.os.Parcel.readArrayMapInternal (Parcel.java:3067)
at android.os.BaseBundle.unparcel (BaseBundle.java:257)
at android.os.BaseBundle.getString (BaseBundle.java:1086)
at sk.myapp.intro.MyFragment.onCreate (MyFragment.java:42)
at android.support.v4.app.Fragment.performCreate (Fragment.java:2414)
at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManagerImpl.java:1418)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManagerImpl.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManagerImpl.java:1852)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange (FragmentManagerImpl.java:3269)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate (FragmentManagerImpl.java:3223)
at android.support.v4.app.FragmentController.dispatchCreate (FragmentController.java:190)
at android.support.v4.app.FragmentActivity.onCreate (FragmentActivity.java:369)
at sk.myapp.intro.MainActivity.onCreate (MainActivity.java:46)
at android.app.Activity.performCreate (Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1221)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11 (Unknown Source)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1696)
at android.os.Handler.dispatchMessage (Handler.java:105)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6942)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)

最佳答案

写这篇文章是因为我无法在评论中提供示例,

@SuppressLint("ParcelCreator")
public class MainActivity extends AppCompatActivity implements CallbackInterface{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Bundle bundle = new Bundle();
bundle.putParcelable("CALLBACK",this); // <---

}

@Override
public void demoCallback() {

}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

}
}

因为我正在传递 this,所以当我的 Activity 包含大量成员变量(> 1MB)时它可能会崩溃

关于android - 将接口(interface)回调添加到 Bundle 后出现 BadParcelableException(非常罕见且随机发生)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56660693/

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