Ad
NullPointerException On Toast
this is my code:
case PlaybackStateCompat.STATE_ERROR: {
mRadioProgress.setVisibility(View.GONE);
mPlayStopButton.setVisibility(View.VISIBLE);
mPlayStopButton.setImageResource(R.drawable.player_play);
Toast.makeText(MainActivity.this, "Streaming not available", Toast.LENGTH_SHORT).show();
break;
}
and this is Crashlytics stack trace:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'void android.app.INotificationManager.enqueueToast(java.lang.String, android.app.ITransientNotification, int)' on a null object reference
at android.widget.Toast.show(Toast.java:286)
Code is in MainActivity, which includes a radio player.Is it because the user has already closed MainActivity, making the context invalid? How can I prevent the crash?
Ad
Answer
Change
Toast.makeText(MainActivity.this, "Streaming not available", Toast.LENGTH_SHORT).show();
to this
Toast.makeText(getApplicationContext(), "Streaming not available", Toast.LENGTH_SHORT).show();
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad