onSaveInstanceState 被调用,但 onCreate 方法中的 savedInstanceState 抛



将一些可序列化的值放到savedinstancestate,但savedInstanceState为空。 onSaveInstanceState(( 使用日志消息调用,代码以相应的方式运行,但在 onCreate 方法中,还原数据后捕获空指针,保存的状态为 null。 类是正确的。(可序列化(已设置。我找不到错误。感谢您的帮助。

@override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mb_activity);
            mBList = new ArrayList<MBClass>();
            mKList = new ArrayList<MKClass>();
            nAList = new ArrayList<AClass>();
            aList = new ArrayList<AClass>();
            try {
                mBList = (ArrayList<MBClass>) savedInstanceState.getSerializable("MBList");
                mKList = (ArrayList<MKClass>) savedInstanceState.getSerializable("MKList");
                aList = (ArrayList<AClass>) savedInstanceState.getSerializable("AList");
                nAList = (ArrayList<AClass>) savedInstanceState.getSerializable("NAList");
                Log.i("MB", "State geschrieben");
            } catch (NullPointerException ne) {
                //ne.printStackTrace();
                Log.i("MB", "State Null");
            }
    }
    @override    
    public void onSaveInstanceState(Bundle savedInstanceState) {
                savedInstanceState.putSerializable("MKList", (Serializable) mKList);
                savedInstanceState.putSerializable("MBList", (Serializable) mBList);
                savedInstanceState.putSerializable("NAList", (Serializable) nAList);
                savedInstanceState.putSerializable("AList", (Serializable) aList);
                super.onSaveInstanceState(savedInstanceState);
                Log.i("MB", "onSaveInstanceState");
            }

Parcelable替换Serializable对我有用。

@Parcelize // this annotation is needed in Kotlin. Not Sure about Java
class DataClass implements Parcelable {
   //Data members
}

相关内容

  • 没有找到相关文章

最新更新