Android片段无法读取包。捆绑包始终为NULL [英] Android fragment can't read bundle. Bundle is always NULL
问题描述
我编写了一个自定义静态方法来创建片段。 Fragment是 android.support.v4.app.Fragment 类的子类。
I wrote a custom static method to create my fragment. Fragment is a subclass of android.support.v4.app.Fragment class.
创建片段的方法如下。
public static AddItemFragment newInstance(UUID listId, UUID itemId){
AddItemFragment fragment=new AddItemFragment();
Bundle bundle=new Bundle();
bundle.putSerializable(EXTRA_DATA_LIST_ID,listId);
bundle.putSerializable(EXTRA_DATA_ITEM_ID, itemId);
fragment.setArguments(bundle);
return fragment;
}
在我的onCreate方法中,我试图从包中读取数据。
In my onCreate method, I am attempting to read data from bundle.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListId = (getArguments().getSerializable(EXTRA_DATA_LIST_ID) == null ? null : (UUID) getArguments().getSerializable(EXTRA_DATA_LIST_ID));
mItemId = (getArguments().getSerializable(EXTRA_DATA_ITEM_ID) == null ? null : (UUID) getArguments().getSerializable(EXTRA_DATA_ITEM_ID));
}
问题是 getArguments()方法永不返回捆绑。它总是返回 NULL 。我不明白为什么。保存实例状态也为NULL。
Well the problem is that getArguments() method never returns bundle. It always returns NULL. I don't understand why. savedInstanceState is NULL as well.
推荐答案
很抱歉,我用Activity的 savedInstanceState 覆盖了片段中设置的捆绑包。捆绑软件,此时为 NULL 。
Silly me was overriding bundle set in the Fragment with Activity's savedInstanceState bundle which at that point is NULL.
DUH ...
这篇关于Android片段无法读取包。捆绑包始终为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!