这就是我现在所做的:
public class AndroidTestToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
private ArrayList<String> itemsList;
private Spinner spinner;
private String contry_name;
private ArrayAdapter<String> dataAdapter;
private TextView textview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("----------",Arrays.toString(Locale.getAvailableLocales()));
itemsList = new ArrayList<String>();
itemsList.add(Arrays.toString(Locale.getAvailableLocales()));
spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener((OnItemSelectedListener) this);
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,itemsList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
textview = (TextView)findViewById(R.id.textView1);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.ENGLISH);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
textview.setText(itemsList.get(position));
}
}
我查看了LogCat,但我不明白什么是错的。
我添加textView
和textView1
后才会抛出异常,但我无法确定问题所在。
这是来自LogCat的错误:
06-07 00:12:58.119: E/AndroidRuntime(12887): FATAL EXCEPTION: main
06-07 00:12:58.119: E/AndroidRuntime(12887): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testotspeech/com.testotspeech.AndroidTestToSpeechActivity}: java.lang.ClassCastException: com.testotspeech.AndroidTestToSpeechActivity
06-07 00:12:58.119: E/AndroidRuntime(12887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
06-07 00:12:58.119: E/AndroidRuntime(12887): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
06-07 00:12:58.119: E/AndroidRuntime(12887): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
06-07 00:12:58.119: E/AndroidRuntime(12887): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
06-07 00:12:58.119: E/AndroidRuntime(12887): at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 00:12:58.119: E/AndroidRuntime(12887): at android.os.Looper.loop(Looper.java:144)
06-07 00:12:58.119: E/AndroidRuntime(12887): at android.app.ActivityThread.main(ActivityThread.java:4937)
06-07 00:12:58.119: E/AndroidRuntime(12887): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 00:12:58.119: E/AndroidRuntime(12887): at java.lang.reflect.Method.invoke(Method.java:521)
06-07 00:12:58.119: E/AndroidRuntime(12887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-07 00:12:58.119: E/AndroidRuntime(12887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-07 00:12:58.119: E/AndroidRuntime(12887): at dalvik.system.NativeStart.main(Native Method)
06-07 00:12:58.119: E/AndroidRuntime(12887): Caused by: java.lang.ClassCastException: com.testotspeech.AndroidTestToSpeechActivity
06-07 00:12:58.119: E/AndroidRuntime(12887): at com.testotspeech.AndroidTestToSpeechActivity.onCreate(AndroidTestToSpeechActivity.java:41)
06-07 00:12:58.119: E/AndroidRuntime(12887): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
06-07 00:12:58.119: E/AndroidRuntime(12887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
06-07 00:12:58.119: E/AndroidRuntime(12887): ... 11 more
答案 0 :(得分:2)
更改此行:textview.setText(itemsList<position>);
进入:textview.setText(itemsList.get(position));
以下是文档:ArrayList.get(...)
答案 1 :(得分:0)
spinner.setOnItemSelectedListener((OnItemSelectedListener) this);
this
)设置为微调器OnItemSelectedListener
,但您的活动未实现该界面。