UI更新问题:
在多线程进行UI更新时候报错android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
分析:
在Android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处理,然后更新用户界面显示。但是,在主线线程之外的线程中直接更新页面显示的问题是
报异常:android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
【只有原始创建这个视图层次(view hierachy)的线程才能修改它的视图(view)】
也就是说必须在一般必须在程序的主线程(也就是ui)线程中进行更新界面显示的工作。可以采用下面的方法之一来解决:
方法一
在onCreate方法中创建一个Handler实例,或者在主线程中创建一个Handler实例,UI刷新的程序放到这个Hanlder实例中执行,如下
public class MainActivity extends Activity {
private ImageView imageView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)this.findViewById(R.id.button);
imageView = (ImageView)this.findViewById(R.id.imageview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
new Thread(){
public void run()
{
try {
InputStream is = HttpUtils.getImageViewInputSream();
if(is!=null)
{
Bitmap bitmap = BitmapFactory.decodeStream(is);
Message msg = new Message();
msg.what = 1;
Bundle bundle = new Bundle();
bundle.putParcelable("bm", bitmap);
msg.setData(bundle);
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
switch (what) {
case 1:
Bundle bundle = msg.getData();
Bitmap bitmap = (Bitmap)bundle.getParcelable("bm");
imageView.setImageBitmap(bitmap);
break;
default:
break;
}
}
};
}
方法二
利用Activity.runOnUiThread(Runnable)把更新UI的代码,创建在Runable中,然后更新UI时,将这个Runable对象传递给Activity.runOnUiThread(Runnable)。这样Runnable对象就能在UI程序中被调用
public class MainActivity extends Activity {
private ImageView imageView;
private Button button;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)this.findViewById(R.id.button);
imageView = (ImageView)this.findViewById(R.id.imageview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
new Thread(){
public void run() {
InputStream is = HttpUtils.getImageViewInputSream();
bitmap = BitmapFactory.decodeStream(is);
if(bitmap!=null)
{
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}