-1

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
unicorn2
  • 844
  • 13
  • 30
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Mar 24 '16 at 13:57
  • first `public static Button b1,b2;` seriously? really bad idea .... now add `Log.d("--", "just before b1.setOnClickListener");` and `Log.d("--", "right after b1=(Button)rootView");` ... now watch the logcat log to see what comes first – Selvin Mar 24 '16 at 14:00

2 Answers2

0

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.

Imdad
  • 683
  • 1
  • 9
  • 27
  • I cant use rootView in the onCreate method and If i call setOnclickListener after that i had assigned the id , it doesnt work anyway . I have already tried. – Ivan Zennaro Mar 27 '16 at 17:05
0

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).

Sevle
  • 3,109
  • 2
  • 19
  • 31
  • I can't use rootView in the onCreate method and If i call setOnclickListener after that i had assigned the id into the PlaceholderFragment, it doesnt work anyway . I have already tried. – Ivan Zennaro Mar 27 '16 at 17:07