Android:ActionBarActivity findViewById return NULL

后端 未结 1 1834
心在旅途
心在旅途 2021-01-16 17:29

I can\'t to get the button from fragment, what\'s wrong? Recieve a uncaught exception - nullpointerexception; Button can\'t be found.

Early all will be find, but aft

1条回答
  •  爱一瞬间的悲伤
    2021-01-16 18:00

    You are trying to search inside Activity's layout, and your widgets are inside a Fragment's layout.

    Put your logic inside your PlaceHolderFragment class:

    public static class PlaceholderFragment extends Fragment implements OnClickListener {
    
        public PlaceholderFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
    
            mplayer = new MediaPlayer();
            rgroup = (RadioGroup) rootView.findViewById(R.id.radioGroup1);
    
            play = (Button) rootView.findViewById(R.id.play);
            pause = (Button) rootView.findViewById(R.id.pause);
    
            play.setOnClickListener(this);
            pause.setOnClickListener(this);
    
            return rootView;
        }
    
        @Override
        public void onClick(View v) {
    
        } 
    }
    

    P.S. Салам алейкум =)

    0 讨论(0)
提交回复
热议问题