试图在setOnUtteranceProgressListener中调用Toaster或Custom函数

时间:2017-03-25 14:29:33

标签: android listener text-to-speech

在我的主要片段中,我实现了TextToSpeech mainFragment extends Fragment implements TextToSpeech.OnInitListener

文字到语音工作正常,我也添加了UtteranceProgressListener正在运作。

我的问题: 我无法调用任何自定义函数,例如gotoNextChapter()甚至是简单的Toaster。 收到错误Can't create handler inside thread that has not called Looper.prepare()

欢迎任何建议或解决方案......

我的代码段:

UtteranceProgressListener SpeechListener = new UtteranceProgressListener() {
            @Override
            public void onStart(String utteranceId) {
                Toast.makeText(getActivity(),utteranceId, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onDone(String utteranceId) {
                Toast.makeText(getActivity(),utteranceId, Toast.LENGTH_LONG).show();

                gotoNextChapter(); <====function I want to call
                read_mode = 0;
            }

            @Override
            public void onError(String utteranceId) {
                Toast.makeText(getActivity(),utteranceId, Toast.LENGTH_LONG).show();
            }
        };

设置话语完整监听器:

myTTS.setOnUtteranceProgressListener(SpeechListener);

得到错误:

Caught a RuntimeException from the binder stub implementation.
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                            at
android.os.Handler.<init>(Handler.java:209)
                                                            at
android.os.Handler.<init>(Handler.java:123)
                                                            at
android.widget.Toast$TN.<init>(Toast.java:350)
                                                            at
android.widget.Toast.<init>(Toast.java:106)
                                                            at
android.widget.Toast.makeText(Toast.java:264)
                                                            at com.MainActivity$Read$10.onDone(MainActivity.java:1252)

2 个答案:

答案 0 :(得分:1)

好的我有一个解决方案,现在我的问题似乎微不足道了

我的更新代码如下所示:

@Override
public void onDone(String utteranceId) {

runOnUiThread(new Runnable() {

                public void run() {
                    Toast.makeText(getContext(),utteranceId,Toast.LENGTH_LONG).show();
gotoNextChapter(); <====function I want to call
read_mode = 0;
                }
            });
}

感谢这些guyes: How can I Toast after Text to Speech finish speaking AndroidWhen may we need to use runOnUiThread in android application?

答案 1 :(得分:1)

如果您在活动中:

runOnUiThread(new Runnable() {
    @Override
    public void run() { 
        Toast.makeText(getActivity(),"...", Toast.LENGTH_LONG).show();  
    }
});

如果你在片段中:

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(getActivity(),"...", Toast.LENGTH_LONG).show();         
    }
});

否则你可以这样做:

Handler mainHandler = new Handler(context.getMainLooper());

Runnable myRunnable = new Runnable() {
    @Override 
    public void run() {
        Toast.makeText(getActivity(),"...", Toast.LENGTH_LONG).show();  
    } 
};
mainHandler.post(myRunnable);