UI更新方法Handler和runOnUiThread

本文探讨了在Android应用开发中遇到的UI更新问题及解决方案。当开发者尝试从非UI线程更新界面时,会遇到CalledFromWrongThreadException异常。文章提供了两种有效的方法来避免这一问题:使用Handler机制或通过runOnUiThread方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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更新解决方法:

也就是说必须在一般必须在程序的主线程(也就是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);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值