Change TextView text

后端 未结 2 1307
轻奢々
轻奢々 2020-12-13 20:40

I\'m trying to change my TextView text from the code.

This is what my xml looks like:

XML:


        
相关标签:
2条回答
  • 2020-12-13 21:02

    Your approach is incorrect. I think it will be Null Pointer Exception (next time post log cat)

    You need to specify the layout you are using first, before finding views.

    Java:

    // Specify the layout you are using.
    setContentView(R.layout.yourlayout);
    
    // Load and use views afterwards
    TextView tv1 = (TextView)findViewById(R.id.textView1);
    tv1.setText("Hello");
    

    Kotlin:

    // Specify the layout you are using.
    setContentView(R.layout.yourlayout)
    
    // Load and use views afterwards
    val tv1: TextView = findViewById(R.id.textView1)
    tv1.text = "Hello"
    

    Click Here to study exactly you want to know

    0 讨论(0)
  • 2020-12-13 21:16

    remove this.. setContentView(tv1);

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