Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to refer to activity element from viewpager
    primarykey
    data
    text
    <p>I try to write an app, that is using slide effect for activities and found that ViewPager from Compatibility Package is what I need. I successfully made the slide, it works in the way i want, but my problem is that I am not able to refer to the elements in my Activity.</p> <p>This is my main.xml:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>MainActivity.java: </p> <pre><code>public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyPagerAdapter adapter = new MyPagerAdapter(); ViewPager myPager = (ViewPager) findViewById(R.id.pager); myPager.setAdapter(adapter); myPager.setCurrentItem(0); if (Fibonacci.getInstance().number != 0) displayResults(); ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark); btnCalculate.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // do button click } }); } 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.activity_dark; break; case 1: resId = R.layout.activity_pale; 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 boolean isViewFromObject(View arg0, Object arg1) { return arg0 == ((View) arg1); } @Override public Parcelable saveState() { return null; } } } </code></pre> <p>My problem is that I receive NullPointerEception for the following line:</p> <pre><code>ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark); </code></pre> <p>I know it's because in setContentView I set up main.xml, and I want to refer to an element in activity_dark.xml or activity_pale.xml. Unfortunately i am not able to figure out how to refer to the element. Any hints would be appreciated! Thank you!</p>
    singulars
    1. This table or related slice is empty.
    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