android片段getArguments()返回null
getArguments()返回null 活动中的代码:android片段getArguments()返回null,android,nullpointerexception,android-fragments,Android,Nullpointerexception,Android Fragments,getArguments()返回null 活动中的代码: if (savedInstanceState == null) { // During initial setup, plug in the details fragment. FlightListFragment listFragment = FlightListFragment.newInstance(mSearchParams);
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);
}
以及:
但我总是在这里得到NullPointerException:getArguments()
请解释我做错了什么。谢谢 编辑:
我发现newInstance()方法是在onCreateView之后调用的,所以我将代码从它移到了onCreate,但问题并没有避免。这意味着在实例化片段时没有提供任何参数 您可以检查是否通过执行以下操作提供了参数
Bundle arguments = getArguments();
if (arguments != null)
{
// then you have arguments
} else {
// no arguments supplied...
}
好的-我误解了你的问题
sp在您正在执行的putParcelable点是否为null
Bundle arguments = getArguments();
if (arguments != null)
{
// then you have arguments
} else {
// no arguments supplied...
}