4
getArguments()返回null!android fragments getArguments()返回null
代碼活動:
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
FlightListFragment listFragment =
FlightListFragment.newInstance(mSearchParams);
getSupportFragmentManager().beginTransaction().add(
android.R.id.content, listFragment).commit();
}
在片段
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
mSearchParams =
getArguments().getParcelable(SearchResultsActivity.EXTRA_SEARCH_PARAMS);
return super.onCreateView(inflater, container, savedInstanceState);
}
和:
public static FlightListFragment newInstance(SearchParams sp) {
FlightListFragment f = new FlightListFragment();
Bundle b = new Bundle();
b.putParcelable(SearchResultsActivity.EXTRA_SEARCH_PARAMS, sp);
f.setArguments(b);
return f;
}
但我始終得到NullPointerException異常這裏:getArguments()。
請解釋我做錯了什麼。
謝謝!
編輯:
我發現在onCreateView之後調用newInstance()方法,所以我將代碼從它移到了onCreate,但問題沒有迴避。
不,sp只在我嘗試恢復時爲空 – rocknow