我知道有很多类似的线程,但我已经遍历了它们,但仍然无法找出问题所在。我的程序到达处理程序,但它总是返回捕获异常“消息未处理。”
我声明了 TextView 私有(private) TextView 聊天框;
在 onCreate 下我有:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpViews();
setUpListener();
}
setUpViews() fragment 看起来像:
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 文件中的 fragment :
<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" />
最佳答案
为了将来引用,我有多个 fragment ,每个 fragment 对应我的 ViewPager 中的每个选项卡。一个 TextView 在两个 fragment 中具有相同的 id
,这造成了冲突。我没有意识到我有相同的 id
所以一切看起来都很好,没有错误消息,但文本根本没有改变。因此,我建议为 id
使用唯一的名称。
关于android TextView setText 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18347016/