簡體   English   中英

Android-需要活動但不想要活動嗎?

[英]Android - Need an activity but don't want an activity?

我正在嘗試創建一個使用textToSpeak的應用程序。 顯然,必須在活動中使用textToSpeak才能使其正常工作。

但是,我將無法在普通的Java類中使用textToSpeak,因此我可以在當前運行的活動中實例化它,並調用使textToSpeech讀出某些文本的方法。

在一個單獨的項目中,我設法使TTS正常工作,這是正在運行的活動,但是我無法在另一個實例中進行實例化...

說話類(包含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

不要擴展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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM