-1

我正在开发文本到语音应用程序。我已经给出了一个输入字符串并通过按钮进行转换。当我单击按钮时,应用程序意外关闭。当输入被硬编码时,应用程序运行良好。

EditText text;
String[] texts ;
TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    texts = new String[3];
    text = (EditText)findViewById(R.id.ttexttovoice);
    //Editable debug = text.getText();
    texts[0] = text.getText().toString();
    Button b = (Button)findViewById(R.id.btexttovoice);
    b.setOnClickListener(this);
    tts = new TextToSpeech(textvoice.this,new TextToSpeech.OnInitListener(){

        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if(status!= TextToSpeech.ERROR)
            {
                tts.setLanguage(Locale.US);

            }
            }
    });

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    if(tts!= null)
    {
        tts.stop();
        tts.shutdown();
    }
    super.onPause();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Random r = new Random();
    String rn = texts[r.nextInt(3)];
    tts.speak(rn, TextToSpeech.QUEUE_FLUSH, null);
}

这里是日志猫:

07-29 07:18:52.635: ERROR/AndroidRuntime(246): Uncaught handler: thread main exiting due to uncaught exception
07-29 07:18:52.665: ERROR/AndroidRuntime(246): java.lang.NullPointerException: println needs a message
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.util.Log.println(Native Method)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.util.Log.i(Log.java:136)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.speech.tts.TextToSpeech.speak(TextToSpeech.java:673)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at org.textvoice.textvoice.onClick(textvoice.java:64)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.view.View.performClick(View.java:2364)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.view.View.onTouchEvent(View.java:4179)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.view.View.dispatchTouchEvent(View.java:3709)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.os.Looper.loop(Looper.java:123)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at java.lang.reflect.Method.invokeNative(Native Method)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at java.lang.reflect.Method.invoke(Method.java:521)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-29 07:18:52.665: ERROR/AndroidRuntime(246):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

我认为 texts[r.nextInt(3)] 是 null.pls 保证 texts[0]~text[2] 有值字符串。

于 2012-07-29T01:40:35.013 回答