如果不支持,在 Text To Speech 中显示 Toast
Showing a Toast in Text To Speech if not Supported
在 Fragement 中将 Toast 添加到 TTS 按钮时,我收到此错误 "Cannot resolve method 'getApplicationContext()" 即使我尝试 getActivty()
。和 getContext()
。 , 然后出现了更多的错误,这是祝酒词 :
Toast.makeText(getApplicationContext(), "Not Supported", 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();
}
}
}
new TextToSpeech(HomeFragment.this
你应该使用 getActivity()
.
Return the FragmentActivity this fragment is currently associated
with.
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();
}
}
});
First of all,
在您的代码中,您给出了 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();
}
}
});
您需要将 HomeFragment.this 更改为 getActivity(),并且还需要将 getApplicationContext() 更改为 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();
}
}
});
Second Option
如果使用 getActivity() Toast 仍然无法正常工作,那么您也可以尝试
getActivity().getBaseContext();
如下:-
Toast.makeText(getActivity().getBaseContext(), "Not Supported", Toast.LENGTH_SHORT).show();
在 Fragement 中将 Toast 添加到 TTS 按钮时,我收到此错误 "Cannot resolve method 'getApplicationContext()" 即使我尝试 getActivty()
。和 getContext()
。 , 然后出现了更多的错误,这是祝酒词 :
Toast.makeText(getApplicationContext(), "Not Supported", 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();
}
}
}
new TextToSpeech(HomeFragment.this
你应该使用 getActivity()
.
Return the FragmentActivity this fragment is currently associated with.
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();
}
}
});
First of all,
在您的代码中,您给出了 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();
}
}
});
您需要将 HomeFragment.this 更改为 getActivity(),并且还需要将 getApplicationContext() 更改为 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();
}
}
});
Second Option
如果使用 getActivity() Toast 仍然无法正常工作,那么您也可以尝试
getActivity().getBaseContext();
如下:-
Toast.makeText(getActivity().getBaseContext(), "Not Supported", Toast.LENGTH_SHORT).show();