按下 home/recent 按钮打开微调器时应用程序崩溃

App crashes when spinner is opened on press of home/recent button

我的应用程序上有一个微调器,当打开微调器然后按下主页或最近按钮时,会导致应用程序崩溃

正在使用的可搜索微调器是 https://github.com/miteshpithadiya/SearchableSpinner

这个错误困扰了我一段时间

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ioksupervisor.ats, PID: 8972
    java.lang.RuntimeException: Parcel: unable to marshal value Select Material
        at android.os.Parcel.writeValue(Parcel.java:1781)
        at android.os.Parcel.writeList(Parcel.java:934)
        at android.os.Parcel.writeValue(Parcel.java:1728)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:846)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
        at android.os.Bundle.writeToParcel(Bundle.java:1232)
        at android.os.Parcel.writeBundle(Parcel.java:886)
        at android.app.FragmentState.writeToParcel(FragmentState.java:120)
        at android.os.Parcel.writeTypedArray(Parcel.java:1471)
        at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:599)
        at android.os.Parcel.writeParcelable(Parcel.java:1800)
        at android.os.Parcel.writeValue(Parcel.java:1706)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:846)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1542)
        at android.os.Bundle.writeToParcel(Bundle.java:1232)
        at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4687)
        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4031)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6626)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

填充微调器

    public void populateSpinner(HashMap<Integer, String> map) {

        List<StringWithTag> arrayList = new ArrayList<StringWithTag>();
        arrayList.add(new StringWithTag("Select Material", 0));

// Hash map contains the key-value pair of the options of the spinner
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            Integer key = Integer.parseInt(String.valueOf(entry.getKey()));
            String value = entry.getValue();
            arrayList.add(new StringWithTag(value, key));
        }

        ArrayAdapter<StringWithTag> arrayAdapter = new ArrayAdapter<StringWithTag>(MainActivity.this, android.R.layout.simple_spinner_item, arrayList);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    }

带标签的字符串 class

public class StringWithTag {

    public String string;
    public Object tag;

    public StringWithTag(String string, Object tag) {
        this.string = string;
        this.tag = tag;
    }

    @Override
    public String toString() {
        return string;
    }
}

提前致谢

仍然对 StringWithTag 执行 Serializable 后,再给我一个错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ioksupervisor.ats, PID: 21320
    java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.toptoche.searchablespinnerlibrary.SearchableSpinner)

这已通过扩展和创建自定义可搜索微调器解决。其中link为https://github.com/miteshpithadiya/SearchableSpinner/issues/55#issuecomment-362430652