Can not find a View with findViewById()

前端 未结 4 1752
执笔经年
执笔经年 2020-11-22 12:19

I cannot find a TextView by calling findViewById(), even though the ID does exist.

OtherActivity:

public class         


        
4条回答
  •  春和景丽
    2020-11-22 12:54

    You have to call setContentView(...) BEFORE you attempt to find any of the UI components. You're trying to find the TextView before you've called it.

    Change your code to this...

    super.onCreate(savedInstanceState);
    setContentView(R.layout.other_activity);
    TextView textView = (TextView)findViewById(R.id.txt02);
    

提交回复
热议问题