-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);
}

これがlogcatです:

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 に答える