Call of findViewById in Fragment to find a button returns null

前端 未结 4 997
遥遥无期
遥遥无期 2021-01-19 00:54

I´m new to android development and I´m trying to write a small, simple Application. This application should do nothing more than reading the text from 2 EditTexts and when

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-19 01:29

    That's the problem

    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString();
            ((TextView) v.findViewById(R.layout.headline_view)).setText(headline);
        }
    });
    

    the View v is the view clicked, so of coure v.findViewById() doesn't work..
    to achieve your target you can declare youre EditText and TextView global, in onCreate() use the inflater to instantiate them and onClick method you can use them!

提交回复
热议问题