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?
onCreateView()? before you'll not have a view