【问题标题】:Showing a Toast in Text To Speech if not Supported如果不支持,则在 Text To Speech 中显示 Toast
【发布时间】:2018-10-12 10:33:43
【问题描述】:

在 Fragment 中将 Toast 添加到 TTS 按钮时,我收到此错误 “无法解析方法 'getApplicationContext()”,即使我尝试了 getActivty()。和getContext()。 ,然后出现更多错误,这是吐司:

Toast.makeText(getApplicationContext(), "不支持", Toast.LENGTH_SHORT).show();

这是片段代码:

  TextToSpeech toSpeech;
    int result;
    EditText editText;
    String text;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, container, false);


        editText = v.findViewById(R.id.editText);

        toSpeech = new TextToSpeech(HomeFragment.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    Locale locale = new Locale("tr-TR");
                    int result = toSpeech.setLanguage(locale);
                } else {
                    Toast.makeText(getApplicationContext(), "Not Supported", Toast.LENGTH_SHORT).show(); 
                }
            }
        });


        return v;
    }

    public void TTS(View view) {
        switch (view.getId()) {
            case R.id.bplay:
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                } else {
                    text = editText.getText().toString();
                    toSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
                    toSpeech.setSpeechRate((float) 0.8);
                    toSpeech.setPitch((float) 0.7);

                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (toSpeech != null) ;
        {
            assert toSpeech != null;
            toSpeech.stop();
            toSpeech.shutdown();
        }

    }

}

【问题讨论】:

标签: android android-fragments text-to-speech android-context android-toast


【解决方案1】:

new TextToSpeech(HomeFragment.this

你应该使用getActivity()

返回这个fragment当前关联的FragmentActivity 与。

Toast.makeText(getActivity(), "Not Supported", Toast.LENGTH_SHORT).show(); 
new TextToSpeech(getActivity()

终于

toSpeech = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                Locale locale = new Locale("tr-TR");
                int result = toSpeech.setLanguage(locale);
            } else {
                Toast.makeText(getActivity(), "Not Supported", Toast.LENGTH_SHORT).show(); 
            }
        }
    });

【讨论】:

    【解决方案2】:

    首先,

    在您的代码中,您给出的 HomeFragment.this 在片段中是不合适的。

    toSpeech = new TextToSpeech(**HomeFragment.this**, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status == TextToSpeech.SUCCESS) {
                        Locale locale = new Locale("tr-TR");
                        int result = toSpeech.setLanguage(locale);
                    } else {
                        Toast.makeText(getApplicationContext(), "Not Supported", Toast.LENGTH_SHORT).show(); 
                    }
                }
            });
    

    您需要使用 getActivity() 更改 HomeFragment.this 并在您的代码中使用 getActivity() 更改 getApplicationContext(),如下所示:-

    toSpeech = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
                    @Override
                    public void onInit(int status) {
                        if (status == TextToSpeech.SUCCESS) {
                            Locale locale = new Locale("tr-TR");
                            int result = toSpeech.setLanguage(locale);
                        } else {
                            Toast.makeText(getActivity(), "Not Supported", Toast.LENGTH_SHORT).show(); 
                        }
                    }
                });
    

    第二个选项

    如果 Toast 使用 getActivity() 仍然无法正常工作,那么您也可以尝试

    getActivity().getBaseContext();
    

    如下:-

    Toast.makeText(getActivity().getBaseContext(), "Not Supported", Toast.LENGTH_SHORT).show();
    

    【讨论】:

    • 第一个也可以,我只是更改了“getActivity”谢谢。
    • @Cristina99 我也给出了更多信息和理解的真实答案。而且您已经先接受了我的回答,对吗?那为什么?
    猜你喜欢
    • 1970-01-01
    • 2019-04-16
    • 2020-01-21
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多