简体   繁体   中英

Android fragment crashes when app comes from background

I'm having a problem that is starting to give me head-hakes.

My application is basically a FragmentActivity with a navigation drawer and each button of the navigation drawer loads a new fragment. I'm using android.support.v4 for almost every component in the project.

My issue lies every time my app goes to background and comes back to foreground the oncreate view loads the view again and most of the variables that I use to create the view are null and my app crashes because of that.

Can anyone point me in the right direction to solve this problem? would it be because of the OnsavedInstanceState() method, the onCreateView() doing the variable instantiation, or anything else?

Here's my error log:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{pt.gema.welcomeangola/pt.gema.welcomeangola.activities.MainActivity}: java.lang.NullPointerException
    Caused by: java.lang.NullPointerException
    at java.util.ArrayList.<init>(ArrayList.java:93)
    at pt.gema.welcomeangola.activities.ListViewExampleFragment.onCreateView(ListViewExampleFragment.java:103)

One of my fragments OnCreateView() Code

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        lvef = this;

        View rootView = inflater.inflate(R.layout.activity_items_list, container, false);
        getActivity().setTitle("ListViewExample");
        btnSearch = (ToggleButton) rootView.findViewById(R.id.btn_search);
        btnSearchText = (ImageButton) rootView.findViewById(R.id.btn_search_string);
        btnAZ = (ToggleButton) rootView.findViewById(R.id.btn_az);
        searchText = (EditText) rootView.findViewById(R.id.search_text);

        layoutSearch = (RelativeLayout) rootView.findViewById(R.id.search_layout);

        pBar = (RelativeLayout) rootView.findViewById(R.id.progress_bar_layout);

        List<Integer> filter= new ArrayList<Integer>();
        filter.add(id_type);

        OriginalObjectsLocality= new ArrayList<ListPlaceInfo>(Objects);
        listview = (ListView) rootView.findViewById(R.id.list_item);

        adapter = new WAListAdapter(getActivity().getApplicationContext(),Objects,OriginalObjectsLocality,lvef);

        listview.setAdapter(adapter);
    return rootView;
}

EDIT Although the problem was not only in the Arraylist Objects, but the instantiation of another class that I tried to access. laalto answer helped me to find the problem, therefore I consider it the right answer.

From comments:

@Szymon I receive that arraylist in the fragment constructor, and i thisnk my problem is here... public ListViewExampleFragment(ArrayList objects) { super(); this.Objects = objects; this.listPlaceAZ=azListing(new ArrayList(objects)); }

You set up a member variable Objects in a constructor that takes an arraylist param.

Fragments must have a parameterless constructor and the framework will create the fragment calling that empty constructor. So your parameter-taking constructor is not called and Objects is left null , causing a NPE here:

OriginalObjectsLocality= new ArrayList<ListPlaceInfo>(Objects);

If you need to pass parameters to your fragments, use a Bundle set with setArguments() and accessed with getArguments() .

But passing an arraylist as a Bundle requires a Parceble and that requires me to change almost all my code, is there any other option?

You could make the member variable static so it survives fragment recreation. But I wouldn't recommend that as it will create a whole set of other problems, such as memory leaks due to incollectible objects.

It's better to rethink the design. For example, you probably don't need to pass an array of objects around. You could just pass an array of identifiers as parameter instead, and query the objects by id when needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM