Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

onClickListener doesnt work in Tabbed Activity Android

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...