我正在制作一个翻译应用程序,我想在其中更新文本视图,但它没有按照我的意愿进行。
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>
我没有得到上述异常的错误。我正在使用具有textview的视图的对象。请帮忙。
答案 0 :(得分:0)
普通工作线程无法修改TextView等UI元素。在UI线程中运行代码at[0].spokentext.setText(spoken);
。在你的run方法中写一下
runOnUiThread(new Runnable() {
@Override
public void run() {//Your code}}
但是我不建议使用这种类型的本机java编码。使用Handler或AsyncTask
答案 1 :(得分:0)
将此用作转移方法:
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
spokentext.post(new Runnable() {
@Override
public void run() {
at[0].spokentext.setText(spoken);
}
});
}
};
t.start();
}
答案 2 :(得分:0)
就像@Pavneet_Singh所说的那样,你无法从另一个线程更新你的UI。您可能希望这样做,而不是调用transfer()
。
public class YourActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
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);
new RetrieveSpokenTextTask().execute();
}
}
});
}
private class RetrieveSpokenTextTask extends AsyncTask<Void,Void,String> {
@Override
public String doInBackground(Void... params) {
String spoken = "the value you would like to assign to the TextView";
return spoken;
}
@Override
public void postExecute(String text) {
// the `text` variable has now the string you returned in `doInBackground`
spokentext.setText(text);
}
}
}