0

这就是我现在所做的:

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仅在添加and后才开始引发异常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
4

2 回答 2

2

将此行更改textview.setText(itemsList<position>);
为:textview.setText(itemsList.get(position));
这是文档:ArrayList.get(...)

于 2012-06-06T21:03:19.197 回答
0

第 41 行:spinner.setOnItemSelectedListener((OnItemSelectedListener) this);
在这一行中,您将当前活动 ( this) 设置为OnItemSelectedListener微调器,但您的活动没有实现该接口。

于 2012-06-06T21:23:39.140 回答