`
gogoalong
  • 浏览: 47613 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

Android:CalledFromWrongThreadException

 
阅读更多

一,更新UI界面

1.1 基本修改组件内容

public class MainActivity extends Activity {
	public TextView mTextTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  
  
         mTextTitle = (TextView) findViewById(R.id._title);  
         mTextTitle.setText("HelloWorld");
	}  
}

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>

在OnCreate()方式中通过mTextTitle.setText("HelloWorld");来修改组件内容。

1.2 子线程中修改UI界面

程序有时会通过线程来进行一些任务,同时需要将结果显示在UI界面。

public class MainActivity extends Activity {
	public TextView mTextTitle;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  
  
         mTextTitle = (TextView) findViewById(R.id._title);  
         thread.start();
	} 

	Thread thread = new Thread() {
		@Override
		public void run() {
			int count = 0;
			for (int i = 0; i < 10; i++) {
				count ++;
			}
			mTextTitle.setText("Sum:" + count);
		}
	};
}

使用上面代码修改UI界面会报错。运行时提示错误:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.解释:Android不允许在子线程修改UI界面,只允许在UI主线程修改UI界面。

1.3 使用Handler通过UI主线程修改界面

public class MainActivity extends Activity {	
	public static final int C_MSG = 100;
	public TextView mTextTitle;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  
  
         mTextTitle = (TextView) findViewById(R.id._title);  
         thread.start();
	}  
	Thread thread = new Thread() {
		@Override
		public void run() {
			int count = 0;
			for (int i = 0; i < 10; i++) {
				count ++;
			}
			Message msg = Message.obtain();
			msg.what = C_MSG;
			msg.arg1 = count;
			handler.sendMessage(msg);
			// mTextTitle.setText("Sum:" + count);
		}
	};
	Handler handler = new Handler(new Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			case C_MSG:
				int count = msg.arg1;
				mTextTitle.setText("Sum:" + count);
				break;
			}
			return false;
		}
	});
}

根据如上代码:通过Handler和消息机制,在UI主线程中修改了TextView的内容。其中使用Handler和Message。有一点可以注意,Message的创建是使用Message msg = Message.obtain();为什么要这么过去对象?

1.4 其他修改UI界面方式

1.4.1 在子线程中通过runOnUiThread()方法更新UI

new Thread() {
	@Override
	public void run() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mTextTitle.setText("HelloWorld--runOnUiThread");
			}
		});	
	}
}.start();

1.4.2 View.post(Runnable r)

mTextTitle.post(new Runnable() {
	@Override
	public void run() {
		mTextView.setText("HelloWorld--View.post()");
	}
});



分享到:
评论

相关推荐

    【Android】CalledFromWrongThreadException 深入源码分析

    先上结论 出现此问题的原因是:在非 UI 线程中创建了 Dialog,而在 UI 线程中调用了 show() 方法 问题还原 在使用 dialog 的时候,因为线程问题,在调用 dismiss() 方法...Caused by: android.view.ViewRootImpl$Called

    问题:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original

    问题:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a 我的场景是 LinearLayout.LayoutParams params11 = new LinearLayout.LayoutParams(264, LinearLayout....

    Android实现在子线程中更新Activity中UI的方法

    本文实例讲述了Android实现在子线程中更新Activity中...ERROR/AndroidRuntime(1222): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its

    详解Android UI更新的几种方法

    如果是在WT进行UI的更新,则会抛出异常,android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.(只有创建这个View的原

    Android 子线程更新UI

    android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 这个异常为何不是 Only the main thread that created a view hierarchy ...

    开新线程引发的常见3个异常

    2.android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 3.java.lang.RuntimeException: Can't create handler inside thread ...

    为什么能在子线程通过setText进行更新UI

    throw new CalledFromWrongThreadException( Only the original thread that created a view hierarchy can touch its views.); } } 一般情况下在子线程更新UI是会报错的,因为在ViewRootImpl

Global site tag (gtag.js) - Google Analytics