onClickListener doesnt work in Tabbed Activity Android

∥☆過路亽.° 提交于 2019-12-12 06:45:57

问题


The setOnclick Listener that I have written in the onCreate method doesn't work. The error is a null pointer exception error for button b1. I tried to initialize b1 before the onclick method but it doesn't work either. For this code I used the Android Studio Example "TAbbed Activity". Then I'm searching a way to use the onClickListener method in a tabbed activity. Please tell me some solutions . Thanks

public class MainActivity extends AppCompatActivity {

        public static Button b1,b2;
        private SectionsPagerAdapter mSectionsPagerAdapter;
        private ViewPager mViewPager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

            mViewPager = (ViewPager) findViewById(R.id.container);
            mViewPager.setAdapter(mSectionsPagerAdapter);


            //  THIS DOESNT RUN AND IT MAKES THE APP CRASH
            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    b1.setText("test");
                }
            });

        }

        public static class PlaceholderFragment extends Fragment {

            private static final String ARG_SECTION_NUMBER = "section_number";
            public PlaceholderFragment() {}

            public static PlaceholderFragment newInstance(int sectionNumber) {
                PlaceholderFragment fragment = new PlaceholderFragment();
                Bundle args = new Bundle();
                args.putInt(ARG_SECTION_NUMBER, sectionNumber);
                fragment.setArguments(args);
                return fragment;
            }

            public void newImageView(int sectionNumber, Button img, int n){
                if(sectionNumber == n)
                    img.setVisibility(View.VISIBLE);
                else
                    img.setVisibility(View.INVISIBLE);
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container, false);

                b1=(Button)rootView.findViewById(R.id.b1);
                newImageView(getArguments().getInt(ARG_SECTION_NUMBER),b1,1);

                b2=(Button) rootView.findViewById(R.id.b2);
                newImageView(getArguments().getInt(ARG_SECTION_NUMBER),b2,2);

                return rootView;
            }
        }

        public class SectionsPagerAdapter extends FragmentPagerAdapter {

            public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int position) {
                return PlaceholderFragment.newInstance(position + 1);
            }

            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "SECTION 1";
                    case 1:
                        return "SECTION 2";
                }
                return null;
            }
        }
    }`enter code here

回答1:


Put this before setting onClickListener

b1=(Button)rootView.findViewById(R.id.b1);

or set listener after you have assigned id. You should assign an id to a button before handling its click. Otherwise, how is android supposed to figure out what button is b1.




回答2:


That button is probably in your fragment's XML (R.layout.fragment_main) so you should add that onClickListener() in the onCreateView() of your PlaceholderFragment after b1=(Button)rootView.findViewById(R.id.b1);.

In any case you cant define an onClickListener() in an object that's not defined first. You first have to find that view usually by the id attribute from the XML with b1=(Button)rootView.findViewById(R.id.b1); or create one using a contructor, eg. b1= new Button(); (not suitable in your case).



来源:https://stackoverflow.com/questions/36201919/onclicklistener-doesnt-work-in-tabbed-activity-android

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