I’m able to build an activity with a viewpager from the various examples on the web, but can I no longer use the views as they were in the original activity? Take for example the following:
public class SimpleViewPagerActivity extends Activity {
LinearLayout thisIsWhatBreaks;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);
thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
thisIsWhatBreaks.setBackgroundColor(Color.BLACK);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
thisIsWhatBreaks.setBackgroundColor(Color.RED);
}
}
public void farLeftButtonClick(View v)
{
Toast.makeText(this, "Far Left Button Clicked", Toast.LENGTH_SHORT).show();
}
public void farRightButtonClick(View v)
{
Toast.makeText(this, "Far Right Elephant Button Clicked", Toast.LENGTH_SHORT).show();
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
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.farleft;
break;
case 1:
resId = R.layout.left;
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
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 originally had the thisIsWhatBreaks LinearLayout in main.xml, but have now moved it to farleft.xml, which is one of the layouts inflated in the PagerAdapter.
Therefore, I can no longer use
LinearLayout thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);
to assign value to the LinearLayout. Where and how do I do this?
How could I once again assign value to the LinearLayout so that it can be modified in the OnConfurationChanged() method as shown within this activity class? I keep getting nullpointerexceptions all over my activity now that I’ve moved most of the views from main.xml to farleft.xml and can’t seem to find a way to make this work.
All I needed to do was move :
thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);to the case which inflates the current layout.xml inside of instantiateItem() of the PagerAdapter.
My problem with the nullpointerexceptions was that all of my views were being acted upon in onCreate(). I had to move all of the views from onCreate() to the case statement inside of instantiateItem();
See the corrected code below….just in case anyone else runs into this problem….