简体   繁体   English

Android-需要活动但不想要活动吗?

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

I am trying to create an app which uses textToSpeak. 我正在尝试创建一个使用textToSpeak的应用程序。 Apparently textToSpeak has to be used within an activity for it to work. 显然,必须在活动中使用textToSpeak才能使其正常工作。

However I wan't to be able to have textToSpeak in a normal java class so I can instantiate it in the currently running activity and call a method which makes the textToSpeech speak out certain text. 但是,我将无法在普通的Java类中使用textToSpeak,因此我可以在当前运行的活动中实例化它,并调用使textToSpeech读出某些文本的方法。

I have managed to get TTS to work when in a separate project and it is the running activity however I cannot get it to instantiate in aa different one... 在一个单独的项目中,我设法使TTS正常工作,这是正在运行的活动,但是我无法在另一个实例中进行实例化...

Speak class (which holds TTS, the problematic line is "private TextToSpeech tts = new TextToSpeech(this, this);" - provides an uncaught exception. 说话类(包含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);
        }
    }

Running activity( Edited to show how I want to use the speech class) 跑步活动(编辑以显示我要如何使用语音课)

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

This set up works fine for other objects however because TTS needs to be part of an activity (it is unrecognized if speech does not extend activity) I doesn't seem to work. 这种设置对于其他对象也很好用,但是因为TTS需要成为活动的一部分(如果语音不扩展活动则无法识别),我似乎没有用。 Can anyone provide a solution to this? 谁能提供解决方案?

LogCat 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

Don't extend Activity . 不要扩展Activity Pass a Context to the constructor instead: 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
}

Then construct it like so: 然后像这样构造它:

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