TextView
という1つのアクティビティで構成されるアプリを作成しました。"ビュー階層を作成した元のスレッドのみがそのビューに触れることができます。エラーが発生しました。
<?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>
私のJavaコードは、このtextView
秒ごとのテキストを設定することです
activity_main.xml:ここに私の活動のXMLです。ここで私は、このタスクを実行するために使用するコードは次のとおりです。
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);
}
}
問題
私は私のアプリをロードし、それを起動起動したときに、私は、次の問題に直面しています私のLG G2の:
私はラインを同定しました。私はいくつかのチェックをしていますか?android.view.ViewRootImpl $ CalledFromWrongThreadException:のみ元のthreビュー階層を作成した広告はそのビューに触れることができます。私はそれを削除するとき、私は例外を持っていないので、エラーの原因となって
timer.schedule(timertask, 0, 1000);
:
JDKでは、 'Text.setText(" test ");を' Platform.runLater(){} 'で囲みます。アンドロイドには同様のものが必要です。 – shinzou