当新线程立即操作UI时,为什么没有CalledFromWrongThreadException? [英] Why is there no CalledFromWrongThreadException when a new thread operates UI immediately?
问题描述
我试图将一个视图动态添加到MainActivity中的另一个视图中:
I was trying to dynamically add a view in to another view in MainActivity:
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout.xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.aronho.testfragment.MainActivity">
<FrameLayout
android:id="@+id/myView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
view_test.xml
view_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF00">
<TextView
android:text="testString"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我知道当我启动应用程序时,此代码将引发CalledFromWrongThreadException:
I know this code will throw CalledFromWrongThreadException when I start the app:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view=vi.inflate(R.layout.view_test,null);
final FrameLayout parentView=(FrameLayout) findViewById(R.id.myView);
parentView.addView(view);
new Thread(new Runnable(){
@Override
public void run() {
try{
Thread.sleep(3000);
}catch (Exception e){
}
parentView.removeView(view);
}
}).start();
}
}
但是当我删除Thread.sleep(3000);时,例如:
but when I remove the Thread.sleep(3000);,eg:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view=vi.inflate(R.layout.view_test,null);
final FrameLayout parentView=(FrameLayout) findViewById(R.id.myView);
parentView.addView(view);
new Thread(new Runnable(){
@Override
public void run() {
parentView.removeView(view);
}
}).start();
}
}
尽管我尝试使用新线程删除视图,但该应用程序不会引发CalledFromWrongThreadException.为什么会发生这种情况?
the app doesn't throw CalledFromWrongThreadException despite I try to remove a view using a new thread. Why would that happen?
推荐答案
崩溃的根本原因是 ViewRootImpl.checkThread()
方法.
The root cause of the crash is the ViewRootImpl.checkThread ()
method.
void checkThread() {
if (mThread != Thread.currentThread()) {
throw new CalledFromWrongThreadException( "Only the original thread that created a view hierarchy can touch its views.");
}
但是,当未测量视图时,不会调用 invalidate()
方法, invalidate()
方法最终将执行到 checkThread()
.
However, the invalidate ()
method is not called when the view is not measured , the invalidate()
method will eventually execute to the checkThread()
.
好,请参阅:
new Thread(new Runnable() {
@Override
public void run() {
System.err.println("getMeasuredHeight:" + parentView.getMeasuredHeight());
System.err.println("getMeasuredWidth:" + parentView.getMeasuredWidth());
parentView.removeView(view);
}
}).start();
将会得到:
W/System.err: getMeasuredWidth:0
W/System.err: getMeasuredHeight:0
可以看到,测量过程尚未完成,因此我们可以在不触发 invalidate()
且不触发异常的情况下更改UI.
can see, the measurement process has not been completed , so we can change the UI without triggering the invalidate()
and not trigger exception.
然后,让它保持睡眠状态50毫秒:
then, keep it sleep 50 ms:
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("getMeasuredHeight:" + parentView.getMeasuredHeight());
System.err.println("getMeasuredWidth:" + parentView.getMeasuredWidth());
parentView.removeView(view);
}
}).start();
将会得到:
W/System.err: getMeasuredHeight:360
W/System.err: getMeasuredWidth:1080
可以看到,测量过程已经完成,然后,当我们更改UI时,将触发 invalidate
,然后调用 checkThread()
.
can see, the measurement process has been completed, then , when we change the UI the invalidate
will be triggering,and then it call the checkThread()
.
这篇关于当新线程立即操作UI时,为什么没有CalledFromWrongThreadException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!