2013-02-16 3 views
0

textToSpeak를 사용하는 앱을 만들려고합니다. 분명히 textToSpeak가 작동하려면 하나의 활동 내에서 사용해야합니다.Android - 활동이 필요하지만 활동을 원하지 않습니까?

그러나 현재 실행중인 활동에서 인스턴스를 만들고 textToSpeech가 특정 텍스트를 말하는 방법을 호출 할 수 있도록 일반 Java 클래스에서 textToSpeak를 가질 수는 없습니다.

TTS를 보유하고

말하기 클래스 (문제가 ... 나는 별도의 프로젝트에 때 작동하는 TTS를 얻기 위해 관리하고 그것을 실행중인 활동이 그러나 나는 그것이 AA 다른 하나의 인스턴스를 얻을 수있다 "개인 TextToSpeech의 TTS = 새로운 TextToSpeech (이,이);"라인입니다 -. 캐치되지 않는 예외를 제공하는 활동을 실행

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(); 
} 
} 
(I는 음성 클래스를 사용하는 방법을 보여주기 위해 수정 됨)

그러나 TTS가 활동의 일부가되어야하기 때문에이 설정은 다른 개체에도 잘 작동합니다 (음성이 활동을 확장하지 않으면 인식되지 않습니다). 작동하지 않는 것 같습니다. 누구나이 솔루션을 제공 할 수 있습니까?

로그 캣

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

확장하지 마십시오 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(); 
    } 
} 
+0

을이 확실히 올바른 방향으로 단계이었다. 더 이상 오류가없고 Logcat 상태가 com.google.android.tts에 연결되어 TTS 서비스에 연결되었습니다. 유일한 문제는 소리가 들리지 않는다는 것입니다 : ( – user2052839

+0

@user2052839 전화가 USB 디버깅 모드입니까? TTS는 말하기 전에 초기화하는 데 시간이 좀 걸릴 수도 있습니다. – Eric

+0

코딩은 에뮬레이터에서 작동합니다 ... 전화 상에없는 이유를 알아 냈습니다. 이게 작동하지 않을뿐만 아니라 TTS가 전화로 작동하지 않는 주요 활동에있는 독립 실행 형 예제입니다 – user2052839

관련 문제