これは私が今やったことです:
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