How do I declare a TextView variable so that I can use it anywhere?

后端 未结 2 414
名媛妹妹
名媛妹妹 2021-01-14 05:20

I have started coding in java/android just today so excuse me if I am being a total idiot here.

I have been facing this problem for the past hour, I have tried to go

2条回答
  •  悲&欢浪女
    2021-01-14 05:35

    You have to instantiate the variable after the setcontentView method was called, so you have to do the following:

    public class MainActivity extends Activity {
    
    TextView test;
    
    @Override
    onCreate(Bundle s){
        ...
        setContentView(R.layout.yourLayout);
        test = (TextView)findViewById(R.id.textView2);
    }
    
    
    public void registermessage(View view) {
    
        test.setText("Test");
    }
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题