Android fragments getArguments () return null

getArguments () returns null!

code in action:

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(); } 

in fragment:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mSearchParams = getArguments().getParcelable(SearchResultsActivity.EXTRA_SEARCH_PARAMS); return super.onCreateView(inflater, container, savedInstanceState); } 

and

 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; } 

But I always get a NullPointerException here: getArguments ().
Please explain to me what I am doing wrong.
Thanks!

Editions:
I found that the newInstance () method is called after onCreateView, so I moved the code from it to onCreate, but the problem could not be avoided.

+4
source share
1 answer

This means that no arguments were specified when creating the instance ...

you can check if the arguments were provided by doing something like this ...

 Bundle arguments = getArguments(); if (arguments != null) { // then you have arguments } else { // no arguments supplied... } 

OK - I misunderstood your question ...

Is sp null at the point you make putParcelable?

+10
source

Source: https://habr.com/ru/post/1435391/


All Articles