大家好,我是Android的新手,我正在构建一个英语到德语的翻译应用程序,我因遵循代码行而收到上述错误:可以协助我如何解决此问题。
package com.exmaple.android.lang_trans;
import java.util.Locale;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.exmaple.android.lang_trans.R;
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
public class MainActivity extends Activity implements OnInitListener {
private TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
((Button) findViewById(R.id.bSpeak)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
speakOut(((TextView) findViewById(R.id.tvTranslatedText)).getText().toString());
}
});
((Button) findViewById(R.id.bTranslate)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
class bgStuff extends AsyncTask<Void, Void, Void> {
String translatedText = "";
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
//below line is throwing the error
String text = ((EditText) findViewById(R.id.etUserText)).getText().toString();
translatedText = translate(text);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
translatedText = e.toString();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
((TextView) findViewById(R.id.tvTranslatedText)).setText(translatedText);
super.onPostExecute(result);
}
}
new bgStuff().execute();
}
});
}
public String translate(String text) throws Exception {
// Set the Client ID / Client Secret once per JVM. It is set statically and applies to all services
Translate.setClientId("CLIENT ID"); //Change this
Translate.setClientSecret("CLIENT SECRET"); //change
String translatedText = "";
translatedText = Translate.execute(text, Language.GERMAN);
return translatedText;
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.GERMAN);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
//speakOut("Ich");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
您的问题是您在 android 系统不允许的 UI 线程以外的线程中访问您的 UI 元素
不能从 UI 线程或"主"线程以外的任何线程更新 UI。
https://developer.android.com/guide/components/processes-and-threads.html#WorkerThreads
因此,您可以将onClick函数更改为以下内容:
((Button) findViewById(R.id.bTranslate)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
class bgStuff extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
if (params.length > 0) {
return translate(params[0]);
}
} catch (Exception e) {
e.printStackTrace();
return e.toString();
}
return null;
}
@Override
protected void onPostExecute(String result) {
((TextView) findViewById(R.id.tvTranslatedText)).setText(result);
}
}
new bgStuff().execute(((EditText) findViewById(R.id.etUserText)).getText().toString());
}
});