私はテキストビューを更新したい翻訳アプリケーションを作っていますが、私が望むようにしていません。テキストビューを即座に更新できません
final audioTranslate at[] = new audioTranslate[1];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_translate);
tv = (TextView) findViewById(R.id.textView11);
spokentext = (TextView) findViewById(R.id.textView12);
translatedtext = (TextView) findViewById(R.id.textView13);
at[0] = this;
start = (Button) findViewById(R.id.Start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v;
if (b.getText().toString().equals("Start")) {
//code
}
else{
tv.setText(""); //these all 3 operations are happening after transfer method is finished.
b.setText("Wait");
b.setEnabled(false);
transfer();
}
}
});
}
void transfer() {
Thread t = new Thread() {
public void run() {
//socket connection
//it is giving exception on below line too.
//Only the original thread that created a view hierarchy can touch its views.
//java.lang.RuntimeException
at[0].spokentext.setText(spoken);
}
};
t.start();
}
これは参照のための私のレイアウトファイルです:
<?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:id="@+id/activity_audio_translate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="diverse.technologies.transcriber.audioTranslate">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Start" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView11"
android:textColor="@color/colorAccent"
android:textSize="24sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView12"
android:textSize="26sp"
android:fontFamily="serif-monospace"
android:textStyle="normal|bold" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5px">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView13"
android:textSize="24sp"/>
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
私は私が取得しています上記の例外と間違っているものを得ていませんよ。私は、テキストビューを持っているビューのオブジェクトを使用しています。助けてください。
問題は正確に説明できますか? – Alvaro
ワーカースレッドから 'UI'を更新できません –
テクスチャtvをコード実行時に更新したいです。そして、私は、クラスをaudiotranslateするならオブジェクトを使ってスレッドからspokentextというテキストビューを更新することができません。 @Pavneet_Singh –