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.
dennisdrew
source share