我知道有很多类似的帖子,但我已经阅读过它们,仍然无法解决问题。我的程序到达了Handler,但它总是返回catch异常“消息未处理”。
我声明了TextView private TextView chatbox;
在onCreate下面我有:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpViews();
setUpListener();
}
setUpViews()代码段如下:
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文件中的片段:
<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" />