Android TextView setText不起作用 [英] android TextView setText not working
问题描述
我知道有很多类似的线程,但是我已经遍历了它们,但仍然无法找出问题所在.我的程序到达Handler,但是它总是返回catch异常消息未处理."
I know there are a lot of similar threads but I've gone through them and still can't figure out the problem. My program reaches the Handler but it always returns the catch exception "Message isn't handled."
我声明了TextView 私有TextView聊天框;
I declared the TextView private TextView chatbox;
在onCreate下,我有:
Under onCreate I have:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpViews();
setUpListener();
}
其中的setUpViews()代码段如下所示:
where setUpViews() snippet looks like:
private void setUpViews() {
chatbox = (TextView) findViewById(R.id.chatbox);
}
处理程序:
public Handler mHandler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg){
try{
chatbox.setText("Got it!");
}catch(Exception e){
Log.i("MYLOG", "Message was not handled.");
}
}
};
main.xml文件中的代码段
Snippet in main.xml file:
<TextView
android:id="@+id/chatbox"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
推荐答案
为将来参考,我有多个片段,其中一个用于ViewPager中的每个选项卡.一个TextView在两个片段中具有相同的 id
,这造成了冲突.我没有意识到我有相同的 id
,所以一切看起来都很好,没有错误消息,但文本只是没有变化.因此,我建议对 id
使用唯一的名称.
For future reference, I had multiple fragments, one for each tab in my ViewPager. One TextView had the same id
in two fragments and that created a conflict. I didn't realize I had the same id
so everything seemed fine, no error message, but the text simply didn't change. Therefore I'd recommend using unique names for the id
.
这篇关于Android TextView setText不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!