2

I have a viewpager with 2 fragment pages inside it. Now i want to access the views (textview,buttons) from the first fragment page but everytime Iam getting Nullpointer exception check out my code

Fragment i want to access its views:

 public class ListPagerFragment extends Fragment { ListView listview; TextView text; private ViewPager pager; public static ListPagerFragment newInstance(int message) { ListPagerFragment f = new ListPagerFragment(); // pass the text message received from main activity into a bundle to be // delivered to the new fragment Bundle bdl = new Bundle(); bdl.putInt("num", message); f.setArguments(bdl); return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.list_pager_fragment, container, false); return v; } public void fillList(Nameslist list) { listview = (ListView) getView().findViewById(R.id.listView1); if (listview == null) System.out.println("listview null"); //this should not be null but //it is thus the nullpointerexception if (listview.getAdapter() == null) { MyListAdapter listAdapter = new MyListAdapter(getActivity(), R.layout.listview_item, list.getVenues()); listview.setAdapter(listAdapter); } else { ((MyListAdapter) listview.getAdapter()).updateAdapter(list.getnames()); } } 

PagerAdapter code

public class MyPagerAdapter extends FragmentStatePagerAdapter { private int mCount = 1; public MyPagerAdapter(FragmentManager fm) { super(fm); // TODO Auto-generated constructor stub } @Override public Fragment getItem(int position) { switch (position) { case 1: return ResultsPagerFragment.newInstance(position); } return ListPagerFragment.newInstance(position); } @Override public int getCount() { // TODO Auto-generated method stub return mCount; } } 

and main finally activity code

private ViewPager pager; private MyPagerAdapter pagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pagerAdapter = new MyPagerAdapter(getSupportFragmentManager()); pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(pagerAdapter); } 

and later somewhere in the activity i have the code that should trigger the fillList from Fragment to do the job

Fragment frag = pagerAdapter.getItem(0); //this should give me the Fragment i want right? ((ListPagerFragment)frag).fillList(result); 

What am I missing here?

1
  • Are you sure your function is called after onCreateView()? before you'll not have a view Commented May 31, 2014 at 2:27

2 Answers 2

1

Ok I found it seems like the problem was in my custom FragmentStatePagerAdapter All I had to do is create the fragments when activity is created and put them into an ArrayList which it will be later passed to MyPagerAdapter

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); frags = new ArrayList<Fragment>(); frags.add(ListPagerFragment.newInstance(0)); frags.add(ResultsPagerFragment.newInstance(1)); pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),frags); pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(pagerAdapter); } 

And then modify the MyPagerAdapter like so:

public class MyPagerAdapter extends FragmentStatePagerAdapter { private List<Fragment> fragments; public MyPagerAdapter(FragmentManager fm, List<Fragment> frags) { super(fm); this.fragments = frags; } @Override public Fragment getItem(int position) { return fragments.get(position); } } 

kind of silly isn't it? I don't understand the point but it worked

Sign up to request clarification or add additional context in comments.

1 Comment

0

That's probably because you are trying to use the views from your Activity, before your ListPagerFragment.onCreateView() has inflated your xml layout. Move what's inside your onCreate() and some code of fillList() to onCreateView(), something like this:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.list_pager_fragment, container, false); listview = (ListView) v.findViewById(R.id.listView1); pagerAdapter = new MyPagerAdapter(getSupportFragmentManager()); pager = (ViewPager) v.findViewById(R.id.pager); pager.setAdapter(pagerAdapter); return v; } 

After your fragments onCreateView() has inflated the view, you can try to fill it.

And don't forget to clear your view references and unregister your listview listener(if you set one) in onDestroyView() to avoid memory leaks, e.g.

@Override public void onDestroyView() { super.onDestroyView(); listview.setOnItemClickListener(null); ... listview = null; } 

read about the fragment's lifecycle --> Fragment's Lifefycle

1 Comment

Thanks I have already considered that possibility. I am pretty sure I don't take any action before the fragment is created. I tried your code but no luck :/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.