2011-02-25 4 views
0

내가 가지고있는 API 데모에 사용되는이 클래스 : 나는 다음 온 클릭 버튼으로 다른 클래스를 가지고 사용하여 sayHello(); 메소드를 호출TextToSpeech의 API - ** - 제발 도와주세요 - ** - 호출 방법

public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener { 

    static int currentHelloIndex = 0; 
private static final String TAG = "TextToSpeechDemo"; 

public static TextToSpeech mTts; 
private Button mAgainButton; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.text_to_speech); 

    // Initialize text-to-speech. This is an asynchronous operation. 
    // The OnInitListener (second argument) is called after initialization completes. 
    mTts = new TextToSpeech(this, 
     this // TextToSpeech.OnInitListener 
     ); 

    // The button is disabled in the layout. 
    // It will be enabled upon initialization of the TTS engine. 
    mAgainButton = (Button) findViewById(R.id.again_button); 

    mAgainButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      sayHello(); 
     } 
    }); 
} 

@Override 
public void onDestroy() { 
    // Don't forget to shutdown! 
    if (mTts != null) { 
     mTts.stop(); 
     mTts.shutdown(); 
    } 

    super.onDestroy(); 
} 

// Implements TextToSpeech.OnInitListener. 
@Override 
public void onInit(int status) { 
    // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR. 
    if (status == TextToSpeech.SUCCESS) { 
     // Set preferred language to US english. 
     // Note that a language may not be available, and the result will indicate this. 
     int result = mTts.setLanguage(Locale.US); 
     // Try this someday for some interesting results. 
     // int result mTts.setLanguage(Locale.FRANCE); 
     if (result == TextToSpeech.LANG_MISSING_DATA || 
      result == TextToSpeech.LANG_NOT_SUPPORTED) { 
      // Lanuage data is missing or the language is not supported. 
      Log.e(TAG, "Language is not available."); 
     } else { 
      // Check the documentation for other possible result codes. 
      // For example, the language may be available for the locale, 
      // but not for the specified country and variant. 

      // The TTS engine has been successfully initialized. 
      // Allow the user to press the button for the app to speak again. 
      mAgainButton.setEnabled(true); 
      // Greet the user. 
      sayHello(); 
     } 
    } else { 
     // Initialization failed. 
     Log.e(TAG, "Could not initialize TextToSpeech."); 
    } 
} 


private static final String[] HELLOS = { 
    "What is a string of words that satisfy the grammatical rules of a sentence", 
    "Salutations", 
    "Greetings", 
    "Howdy", 
    "What's crack-a-lackin?", 
    "That explains the stench!" 
}; 


public static void sayHello() { 
    // Select a random hello. 

    int helloLength = HELLOS.length; 
    String hello = HELLOS[currentHelloIndex]; 
    currentHelloIndex = (currentHelloIndex + 1) % helloLength; 
    mTts.speak(hello, 
     TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue. 
     null); 


    } 

} 

TextToSpeech.sayHello(); 이렇게하면 프로세스 폐쇄 오류가 발생하고 logcat 소스에서 널 예외 오류가 발생합니다. 누군가이 문제를 해결할 수 있습니까? 감사합니다

답변

0

현재 디자인에는 중대한 결함이 있습니다. mTtsonCreate 메서드가 TextToSpeechActivity 인 경우에만 초기화되지만 다른 클래스의 static 메서드에서이 개체에 대한 메서드를 호출하면됩니다. 다른 클래스가 sayHello() 일 때 mTtsnull이 아닌지 확인할 방법이 없습니다. 또한 mTts이 null이 아니더라도 TextToSpeechActivity의 인스턴스 인 생성자로 this이 전달되었으므로 TextToSpeech이 완전히 다른 경우 Activity으로 만들 때 작동하는지 알 수 없습니다.

클래스 디자인을 재고해야한다고 생각합니다.

+0

감사합니다. 그래서 다른 클래스의 SayHello 메서드를 호출 할 방법이 없습니까? – Tommy

+0

dave에게 도움을 주셔서 감사합니다. mTts = main.mTts와 같은 주요 활동에서 변수를 가져 와서 main에서 currentHelloIndex를 호출하고이를 새로운 int에 저장하는 변수를 작성하여이 작업을 수행했습니다. 당신의 도움을 주셔서 감사합니다. – Tommy

관련 문제