Android ViewPager findViewById not working - always returns null

public class HSPTabletTestActivity extends Activity { private class MyPagerAdapter extends PagerAdapter { public int getCount() { return 2; } public Object instantiateItem(View collection, int position) { LayoutInflater inflater = (LayoutInflater) collection.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); int resId = 0; switch (position) { case 0: resId = R.layout.lighttab; break; case 1: resId = R.layout.securitytab; break; } View view = inflater.inflate(resId, null); ((ViewPager) collection).addView(view, 0); return view; } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView((View) arg2); } @Override public void finishUpdate(View arg0) { // TODO Auto-generated method stub } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == ((View) arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { // TODO Auto-generated method stub } @Override public Parcelable saveState() { // TODO Auto-generated method stub return null; } @Override public void startUpdate(View arg0) { // TODO Auto-generated method stub } } 

I have this code there ^^ ... Quite a lot is taken directly from http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/ ... I'm still quite new in the world of android development: /

Now I'm trying to access the spinner control and some buttons inside the "pages". But findViewById continues to return null!

I remember something about the fact that the layout doesn’t really exist inside the code and should be overstated first, which happens in the instantiateItem () function. And it is pretty obvious that it enters the "view" variable. But when I call view.findViewById (R.id.light1_off);

which is a button, by the way, it always returns ZERO! I even made sure that it only called when the page was actually loaded. But no matter what I did, it always returns null, and I get an unpleasant nullpointer exception.

Can anyone help me here? I have a lot of ideas, and Google does not help, already on page 5 for about 10 different search queries.

+8
android android-layout
source share
3 answers

After much frustration and hair pulling on this issue, I solved it! At least for me. Assuming you used the tutsplus tutorial, just like me, you have separate XML files for your screens. Now I assume that these XML layouts contain layouts inside them (e.g. LinearLayout, RelativeLayout, etc.). Now these layouts contain your button and other widgets. What you need to do to be able to findViewById, in the actual switch statement in the instatiateItem method, initialize your button as follows:

 public Object instantiateItem(View collection, int position) { LayoutInflater inflater = (LayoutInflater) collection.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); int resId = 0; switch (position) { case 0: resId = R.layout.lighttab; View view = inflater.inflate(resId, null); RelativeLayout layout=(RelativeLayout)view.findViewById(R.id.relLayout); Button button=(Button)layout.findViewById(R.id.button); ((ViewPager) collection).addView(view, 0); return view; case 1: resId = R.layout.securitytab; ... ... return view; } } 

So ... first you need to inflate the view, and then initialize the layout held in the view by casting the layout type (RelativeLayout) and calling .findViewById (resource identifier). Then you initialize the actual widget that you are looking for, doing the same thing, but throwing the widget view (Button) and calling .findViewById (resource identifier).

It worked for me, so I hope it saves you the trouble! Took me forever to understand.

+21
source share

Just guess, since you are not showing xml for bloat. Do you use + @id. If not try.

0
source share

You can avoid this dirty work by simply adding setContentView (R.layout.youlayout) before findViewById

0
source share

All Articles