getArguments() 返回空值!
活动代码:
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,但问题并没有避免。