[英]Showing a Toast in Text To Speech if not Supported
在Fragement中将Toast添加到TTS按钮时,即使尝试了getActivty()
,我也会收到此错误“无法解析方法'getApplicationContext()” 。 和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();
}
}
}
新的TextToSpeech(HomeFragment.this
您应该使用getActivity()
。
返回此片段当前与之关联的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();
}
}
});
首先,
在您的代码中,您给出了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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.