When adding Toast to TTS button in Fragement , i get this error "Cannot resolve method 'getApplicationContext()" even i tried getActivty()
. and getContext()
. , then more errors showed up,this is the toast :
Toast.makeText(getApplicationContext(), "Not Supported", Toast.LENGTH_SHORT).show();
Here is the fragement code :
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
You should use getActivity()
.
Return the FragmentActivity this fragment is currently associated with.
Toast.makeText(getActivity(), "Not Supported", Toast.LENGTH_SHORT).show();
new TextToSpeech(getActivity()
Finally
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,
In your code you have given HomeFragment.this which is not proper in fragment.
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();
}
}
});
You need to change HomeFragment.this with getActivity() and also change getApplicationContext() with getActivity() in your code like below : -
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
If in case still Toast is not working properly using getActivity() then you can also try
getActivity().getBaseContext();
like below:-
Toast.makeText(getActivity().getBaseContext(), "Not Supported", Toast.LENGTH_SHORT).show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.