1,错误一:当点击按钮运行这个方法时,出现错误如下
/**
* 新线程1(错误)
*/
public void newThread() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
text.setText("哈哈");
}
}).start();
}
12-26 16:59:40.188: E/AndroidRuntime(11545): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4109)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.view.View.requestLayout(View.java:12715)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.view.View.requestLayout(View.java:12715)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.view.View.requestLayout(View.java:12715)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.view.View.requestLayout(View.java:12715)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.view.View.requestLayout(View.java:12715)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.widget.TextView.checkForRelayout(TextView.java:6864)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.widget.TextView.setText(TextView.java:3375)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.widget.TextView.setText(TextView.java:3177)
12-26 16:59:40.188: E/AndroidRuntime(11545): at android.widget.TextView.setText(TextView.java:3152)
12-26 16:59:40.188: E/AndroidRuntime(11545): at com.example.zza_android_test6.MainActivity$4.run(MainActivity.java:153)
12-26 16:59:40.188: E/AndroidRuntime(11545): at java.lang.Thread.run(Thread.java:856)
解决方法:
/**
* 新线程1(正确)
*/
public void newThread() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
text.setText("哈哈");
}
});
}
}).start();
}
2、错误二:同样出现上述的错误
/**
* 新线程2(错误)
*/
public void newThread2() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
text.setText("华灯初上");
}
}).start();
}
解决方法:通过handler发送消息给主线程处理
/**
* 新线程2(正确)
*/
public void newThread2() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message=handler.obtainMessage();
message.what=1;
handler.sendMessage(message);
}
}).start();
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
text.setText("华灯初上");
break;
default:
break;
}
};
};
Android更新UI的两种方法——handler与runOnUiThread()
在Android开发过程中,常需要更新界面的UI。而更新UI是要主线程来更新的,即UI线程更新。如果在主线线程之外的线程中直接更新页面 显示常会报错。抛出异常:android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
只有原始创建这个视图层次(view hierachy)的线程才能修改它的视图(view)
话不多说,贴出下面的代码
方法一:
在Activity.onCreate(Bundle savedInstanceState)中创建一个Handler类的实例, 在这个Handler实例的handleMessage回调函数中调用更新界面显示的函数。
界面:
public class MainActivity extends Activity {
private EditText UITxt;
private Button updateUIBtn;
private UIHandler UIhandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UITxt = (EditText)findViewById(R.id.ui_txt);
updateUIBtn = (Button)findViewById(R.id.update_ui_btn);
updateUIBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
UIhandler = new UIHandler();
UIThread thread = new UIThread();
thread.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class UIHandler extends Handler{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle bundle = msg.getData();
String color = bundle.getString("color");
UITxt.setText(color);
}
}
private class UIThread extends Thread{
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putString("color", "黄色");
msg.setData(bundle);
MainActivity.this.UIhandler.sendMessage(msg);
}
}
}
更新后:
方法二:利用Activity.runOnUiThread(Runnable)把更新ui的代码创建在Runnable中,然后在需要更新 ui时,把这个Runnable对象传给Activity.runOnUiThread(Runnable)。 这样Runnable对像就能在ui程序中被调用。如果当前线程是UI线程,那么行动是立即执行。如果当前线程不是UI线程,操作是发布到事件队列的UI 线程
FusionField.currentActivity.runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(getApplicationContext(), , "Update My UI",
Toast.LENGTH_LONG).show();
}
});