how to get access of my button in one of my viewpager pages?

匿名 (未验证) 提交于 2019-12-03 01:22:02

问题:

I'm new to this android and java.

My viewpager now can slide all my xml layout, but i don't know how to access item in them.

I tried using Button mybutton = (Button) findViewById (R.layout.result), then set the onClickListener in mybutton, but then it was forced to close every time I run it.

Here is my code:

public class main extends Activity {      private ViewPager myviewpager;     private ViewPagerAdapter myviewpageradapter;     private static int NUM_AWESOME_VIEWS = 12;     Button button_1;        @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          myviewpageradapter = new ViewPagerAdapter();         myviewpager = (ViewPager) findViewById(R.id.myviewpager);         myviewpager.setAdapter(myviewpageradapter);              button_1 = (Button) myviewpager.findViewById(R.id.first_button);             button_1.setOnClickListener(new OnClickListener() {                 public void onClick(View v) {                     finish();                 }             });     }      private class ViewPagerAdapter extends PagerAdapter {          @Override         public int getCount() {                 return NUM_AWESOME_VIEWS;         }          @Override         public Object instantiateItem(View collection, int position) {                 LayoutInflater layoutinflater = getLayoutInflater();                  View view;                 view = layoutinflater.inflate(R.layout.page1, null);                  switch (position)                 {                     case 0 : view = layoutinflater.inflate(R.layout.page1, null); break;                     case 1 : view = layoutinflater.inflate(R.layout.page2, null); break;                     case 2 : view = layoutinflater.inflate(R.layout.page3, null); break;                     case 3 : view = layoutinflater.inflate(R.layout.page4, null); break;                     case 4 : view = layoutinflater.inflate(R.layout.page5, null); break;                     case 5 : view = layoutinflater.inflate(R.layout.page6, null); break;                     case 6 : view = layoutinflater.inflate(R.layout.page7, null); break;                     case 7 : view = layoutinflater.inflate(R.layout.page8, null); break;                     case 8 : view = layoutinflater.inflate(R.layout.page9, null); break;                     case 9 : view = layoutinflater.inflate(R.layout.page10, null); break;                     case 10 : view = layoutinflater.inflate(R.layout.page11, null); break;                     case 11 : view = layoutinflater.inflate(R.layout.result, null);break;                 }                 ((ViewPager) collection).addView(view,0);                 return view;         }          @Override         public void destroyItem(View collection, int position, Object view) {                 ((ViewPager) collection).removeView((View) view);         }            @Override         public boolean isViewFromObject(View view, Object object) {                 return view==((View)object);         }          @Override         public void finishUpdate(View arg0) {}           @Override         public void restoreState(Parcelable arg0, ClassLoader arg1) {}          @Override         public Parcelable saveState() {                 return null;         }          @Override         public void startUpdate(View arg0) {}      }

回答1:

It may just be the Problem, that your items are not found probably so try this(If your view is no LinearLayout change it)

LayoutInflater getView = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  LinearLayout view1 =(LineaLayout)getView.inflate(R.layout.your_view,null);

But Your Button may throw Exception furthermore there is an onClickListener set to it. If it's just the Button define it in XML:

android:onClick"buttonClick"

then in Code

public void buttonClick(View v){  // do something  }

If this is not possible because you need to reference the Button later, I stuck with the same Problem and would like to know how it works.



回答2:

I think the problem here is that you are trying to find a view (findViewById) that wasn't instatiated (inflated) yet. Try to set the button listener inside the instatiateItem method of ViewPagerAdapter type.

Example:

    switch(position){     case 0 :      view = layoutinflater.inflate(R.layout.page1, null);      button_1 = (Button) view.findViewById(R.id.first_button);     button_1.setOnClickListener(new OnClickListener() {          public void onClick(View v) {                finish();                }          });     break;     (...)         }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!