Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid viewpager......how to get a hold of the views?
    primarykey
    data
    text
    <p>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:</p> <pre><code>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 } } } </code></pre> <p>I originally had the <code>thisIsWhatBreaks</code> LinearLayout in <code>main.xml</code>, but have now moved it to <code>farleft.xml</code>, which is one of the layouts inflated in the PagerAdapter.</p> <p>Therefore, I can no longer use <code>LinearLayout thisIsWhatBreaks = (LinearLayout)findViewById(R.id.layoutThatsAlwaysNull);</code> to assign value to the LinearLayout. Where and how do I do this?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload