textView setText() NullPointerException

前端 未结 3 1400
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 12:13

I have an almost completely Vanilla App. All I\'m trying to do is change the text of a textView, but it gives me a NullPointerException. I have no XML configuration, no added me

3条回答
  •  -上瘾入骨i
    2021-01-22 12:58

    Can you post xml? It's likely that the id your java is assuming "R.id.textView1" is wrong. Maybe R.id.textview1?

    Well if your text view lives inside the fragment just do this:

    @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container,
                        false);
                TextView tv = (TextView) rootView.findViewById(R.id.textView1);
            tv.setText("Teststring");
                return rootView;
            }
    

提交回复
热议问题