Android - 需要一项活动,但不想要一项活动?

时间:2013-02-16 16:56:46

标签: java android exception android-activity text-to-speech

我正在尝试创建一个使用textToSpeak的应用。显然textToSpeak必须在活动中使用才能工作。

但是我不能在普通的java类中使用textToSpeak,所以我可以在当前运行的活动中实例化它并调用一个方法,使textToSpeech说出某些文本。

我已经设法让TTS在单独的项目中工作,而且它是正在运行的活动但是我不能让它在另一个实例中实例化...

Speak class(包含TTS,有问题的行是“private TextToSpeech tts = new TextToSpeech(this,this);” - 提供未捕获的异常。

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class Speech extends Activity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts = new TextToSpeech(this, this);
    private String toRead;

    public void speak()
    {
        speakOut();
    }

    public void changeText(String changeTo)
    {
        toRead = changeTo;
    }

    @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.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }

    private void speakOut() {
        tts.speak(toRead, TextToSpeech.QUEUE_FLUSH, null);
        }
    }

正在运行的活动(编辑以显示我想如何使用语音课程)

public class Text_entry extends Activity implements OnTouchListener{
private Speech speech = new Speech();

public boolean onTouch(View v, MotionEvent event) {
speech.changeText(toRead);
        speech.speak();
}
}

这个设置适用于其他对象但是因为TTS需要是活动的一部分(如果语音不扩展活动就无法识别)我似乎不起作用。任何人都可以提供解决方案吗?

logcat的

02-16 16:52:46.698: D/AndroidRuntime(22541): Shutting down VM
02-16 16:52:46.698: W/dalvikvm(22541): threadid=1: thread exiting with uncaught exception (group=0x415fe300)
02-16 16:52:46.703: E/AndroidRuntime(22541): FATAL EXCEPTION: main
02-16 16:52:46.703: E/AndroidRuntime(22541): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.BT/org.BT.Text_entry}: java.lang.NullPointerException
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.os.Looper.loop(Looper.java:137)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at java.lang.reflect.Method.invokeNative(Native Method)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at java.lang.reflect.Method.invoke(Method.java:511)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at dalvik.system.NativeStart.main(Native Method)
02-16 16:52:46.703: E/AndroidRuntime(22541): Caused by: java.lang.NullPointerException
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:91)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.speech.tts.TtsEngines.getDefaultEngine(TtsEngines.java:75)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.speech.tts.TextToSpeech.getDefaultEngine(TextToSpeech.java:1235)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:595)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:553)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:527)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:512)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at org.BT.Speech.<init>(Speech.java:12)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at org.BT.Text_entry.<init>(Text_entry.java:48)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at java.lang.Class.newInstanceImpl(Native Method)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at java.lang.Class.newInstance(Class.java:1319)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
02-16 16:52:46.703: E/AndroidRuntime(22541):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
02-16 16:52:46.703: E/AndroidRuntime(22541):    ... 11 more

1 个答案:

答案 0 :(得分:1)

不要延长Activity。将Context传递给构造函数:

public class Speech implements TextToSpeech.OnInitListener {
    private Context mContext;
    private TextToSpeech tts;

    public Speech(Context c) {
        mContext = c;
        tts = new TextToSpeech(c, this);
    }

    // Rest of class
}

然后像这样构建它:

public class Text_entry extends Activity implements OnTouchListener{
    private Speech speech;

    public void onCreate(Bundled saved) {
        super.onCreate(saved);
        speech = new Speech(this);
    }
    public boolean onTouch(View v, MotionEvent event) {
        speech.changeText(toRead);
        speech.speak();
    }
}