对话框中的TTS崩溃应用程序

时间:2015-07-01 00:58:12

标签: android

尝试在警告对话框中实现TextToSpeech。来自EditText。文本将转换为String,但运行该对话框会导致应用程序崩溃。 这是源代码:

我在全球宣布public TextToSpeech mTts;。在活动中,我使用OnClick调用了对话框。

public void convertText(View view)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("TEXT TO SPEECH");

    mTts.setLanguage(Locale.US);




 // Set up the input
  final   EditText input = new EditText(this);
 // Specify the type of input expected
    input.setInputType(InputType.TYPE_CLASS_TEXT | 
    InputType.TYPE_CLASS_TEXT);
    builder.setView(input);

 // Set up the buttons
    builder.setPositiveButton("OK", new 
    DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //m_Text = input.getText().toString();
        }
    });
    builder.setNegativeButton("Play", new 
    DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String toSpeak = input.getText().toString();
            mTts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);


        }
    });

    builder.show();
}

这也是Logcat:

> 07-01 06:15:06.608    1850-1850/zyia.alarm.zyia.zyia E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: zyia.alarm.zyia.zyia, PID: 1850
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.setLanguage(java.util.Locale)' on a null object reference
            at zyia.alarm.zyia.zyia.AddAlarm.convertText(AddAlarm.java:157)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

是什么让它崩溃?任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

就像@ isma3l所说的那样,你可能没有初始化mTts

public TextToSpeech mTts = new TextToSpeech(this, this);

此外,另一个可能触发此错误的问题是没有实现TextToSpeech.OnInitListener以通知初始化完成。

public class MainActivity 
    extends AppCompatActivity 
    implements TextToSpeech.OnInitListener{

public TextToSpeech mTts;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTts = new TextToSpeech(this, this);
}

@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        mTts.setLanguage(Locale.getDefault());
    } else {
        mTts = null;
        Log.e("MainActivity", "Failed to initialize the TextToSpeech engine");
    }
}