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 ?