Java W/TextToSpeech:speak失败:未绑定到TTS引擎

Java W/TextToSpeech:speak失败:未绑定到TTS引擎,java,android,android-fragments,text-to-speech,google-text-to-speech,Java,Android,Android Fragments,Text To Speech,Google Text To Speech,我有我的类MyTTS,我有方法speakout。当我在类内部调用它时,它工作得很好,但是如果我在其他类中初始化这个类,并且我再次调用这个方法时,它会给我 W/TextToSpeech:speak失败:未绑定到TTS引擎 这是我的类MyTTS.java: TextToSpeech textToSpeech; public MyTTS(Context context) { textToSpeech=new TextToSpeech(context,this); } @Require

我有我的类
MyTTS
,我有方法
speakout
。当我在类内部调用它时,它工作得很好,但是如果我在其他类中初始化这个类,并且我再次调用这个方法时,它会给我

W/TextToSpeech:speak失败:未绑定到TTS引擎

这是我的类MyTTS.java:

TextToSpeech textToSpeech;


public MyTTS(Context context) {
    textToSpeech=new TextToSpeech(context,this);
}



@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void speakOut(String str,String pk){
    textToSpeech.speak(str,TextToSpeech.QUEUE_FLUSH,null,pk);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {

        int result = textToSpeech.setLanguage(Locale.US);

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {
           speakOut("badr","dfd");
        }

    } else {
        Log.e("TTS", "Initilization Failed!");
    }
}

@Override
public void onPause() {

    if(textToSpeech==null){
        textToSpeech.stop();
        textToSpeech.shutdown();

    }

    super.onPause();
}
这就是我初始化MyTTS类并调用speakOut方法的类:

fragment.java:

public class must_adapter_frag extends Fragment{
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("JavascriptInterface")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //Inflate the layout for this fragment

        MyTTS myTTS=new MyTTS(getActivity());

        View view= inflater.inflate(R.layout.webview_frag, container, false);
        System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
        myTTS.speakOut("salam","dj");

        System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
        WebView webView=view.findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(myTTS,"Android");
        webView.loadUrl("file:///android_asset/"+String.valueOf(getArguments().getInt("position"))+".html");



        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().setTitle("تعليق");

    }
如果有专家能帮我解决这个问题,请


我很抱歉我的英语不是我的语言。

您会遇到这个错误,因为MyTTS对象中的TextToSpeech对象(您从片段中创建的)尚未初始化

在MyTTS类中,您可以看到在onInit()方法中有speakOut()。这就是为什么它在那里能正常工作。。。因为只有在初始化textToSpeech对象后才会调用onInit

所以。。。你能做的是:

public boolean textToSpeechIsInitialized = false;  // <--- add this line

public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {

        textToSpeechIsInitialized = true;  // <--- add this line

        int result = textToSpeech.setLanguage(Locale.US);

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {
           speakOut("badr","dfd");
        }

    } else {
        Log.e("TTS", "Initilization Failed!");
    }
}
public boolean texttospeechisinialized=false//
System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
        if (myTTS.textToSpeechIsInitialized) {  // <--- add this line
             myTTS.speakOut("salam","dj");
        } else { 
             // try again later 
        }