Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Fragment null pointer when fetching view
    text
    copied!<p>I have been hitting a wall with Fragments and triggering a view refresh when the adapter changes for a listview inside the fragment. Right now I have:</p> <ul> <li>A main activity which holds the FragmentManager</li> <li>The activity creates fragments based on position through the FragmentPagerAdapter.getItem method</li> <li>A subsequent activity or button press causes the data behind one of the Fragments to change</li> <li>The activity calls the refresh method on the custom Fragment class to trigger the data refresh</li> <li>The refresh method fetches the ListView object in the fragment <ul> <li>This is where the Null exception occurs</li> </ul></li> </ul> <p>The refresh method is below:</p> <pre><code>public void refresh() { groups = new Select() .from(Group.class) .execute(); listView = (ListView)getActivity().findViewById(R.id.overview_container); // Never get here groupAdapter.clear(); groupAdapter.setItems(groups); listView.invalidateViews(); } </code></pre> <p>The setup is</p> <pre><code>public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_overview, container, false); groups = new Select() .from(Group.class) .execute(); listView = (ListView)view.findViewById(R.id.overview_container); listView.setAdapter(new GroupAdapter(view.getContext(), R.layout.time_view, groups)); return view; } </code></pre> <p>Is there something fundamental I am missing here? Is there an easier pattern here like listeners that can work around this issue? I just can't seem to get the view to return anything but null - even if I store it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload