我正在尝试TextView从代码中更改我的文本.
这就是我的xml的样子:
XML:
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" />
Run Code Online (Sandbox Code Playgroud)
和代码:
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
setContentView(tv1);
Run Code Online (Sandbox Code Playgroud)
我的设备出现错误,应用程序停止运行.我试图显示一个TextView(没有连接到XML TextView),它工作.
Abh*_*bhi 49
你的方法不正确.我认为这将是Null Pointer Exception.最好发布日志猫
在这里纠正
setContentView(R.layout.yourlayout):
Run Code Online (Sandbox Code Playgroud)
在此行中,您应指定正在使用的布局.
然后
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
Run Code Online (Sandbox Code Playgroud)
在这里学习这是你想知道的
| 归档时间: |
|
| 查看次数: |
63303 次 |
| 最近记录: |