Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android fragments getArguments() return null

getArguments() return null!

code in activity:

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 NullPointerException here: getArguments().
Please, explain me what I am doing wrong.
Thanks!

EDITED:
I've found that newInstance() method is called after onCreateView, so I moved code from it to onCreate, but the issue didn't avoid.

like image 438
rocknow Avatar asked Dec 26 '22 17:12

rocknow


1 Answers

It means no arguments were supplied when the fragment was instantiated...

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

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

OK - I misread your question...

Is sp null at the point your doing putParcelable?

like image 62
0909EM Avatar answered Jan 07 '23 21:01

0909EM