问题
I have created an app composed of one single activity which include a single TextView
. Here is the XML of my activity :
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="zeratops.myApp.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#000000"
android:layout_alignParentTop="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text=""
android:textColor="#FF0000"
android:id="@+id/text_1"
android:paddingTop="200dp"
android:layout_gravity="center" />
</FrameLayout>
</RelativeLayout>
My java code is to set the text of this textView
every second. Here is the code I use to perform this task :
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView) findViewById(R.id.text_1);
Timer timer = new Timer();
TimerTask timertask = new TimerTask() {
@Override
public void run() {
text.setText("test");
}
};
timer.schedule(timertask, 0, 1000);
}
}
The issue
I am facing the following issue when I launch load my app and launch it on my LG G2 :
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
I identified the lines :
timer.schedule(timertask, 0, 1000);
causing the error since I do not have exceptions when I remove it. Am I mising some checking ?
回答1:
Your run()
method will be called on a background thread, and it cannot manipulate the UI as a result.
A lighter-weight solution would be:
public class MainActivity extends AppCompatActivity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text_1);
onEverySecond.run();
}
private Runnable onEverySecond=new Runnable() {
@Override
public void run() {
text.setText("test");
text.postDelayed(this, 1000);
}
}
}
postDelayed()
calls your run()
method on the main application thread, after the designated delay period. Here, we use that to have the Runnable
schedule itself to run again after 1000ms.
To stop the postDelayed()
"loop", call removeCallbacks(onEverySecond)
on your MainActivity
.
The advantage of this over using runOnUiThread()
inside the TimerTask
is that it is less expensive, as you are not creating a separate thread (the Timer
/TimerTask
thread).
回答2:
You need to perform
text.setText("test");
in UI thread. You can do so using Handlers.
final Handler myHandler = new Handler();
myHandler.post(myRunnable);
final Runnable myRunnable = new Runnable() {
public void run() {
text.setText("test");
}
};
来源:https://stackoverflow.com/questions/39541081/only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-views-er