I have an android app which crashes when it receives notification. The notification receive messages from my app server.Tapping the notification opens up a webview activity. My problem is when I hardcode an url link to open in the webview, it works fine. But, when I use a URL link (in string value) sent from my app server and save it in shared preferences and then use it to start the webview activity the app crashes.
The code is as follows:-
This is the GCMPushReceiverClass where I receive messages and send notification:-
public class GCMPushReceiverService extends GcmListenerService {
Context applicationContent = FirstActivity.getApplicationData();
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
String link = data.getString("link");
sendNotification(message);
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(applicationContent);
SharedPreferences.Editor writer = mPrefs.edit();
writer.putString("LINK",link);
writer.commit();
}
private void sendNotification(String message) {
Intent intent = new Intent(this, ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestCode = 0;
Random random = new Random();
int m = random.nextInt(9999-1000)+1000;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
int color = getResources().getColor(R.color.notColour);
NotificationCompat.Builder notBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.sovran_icon)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(1)
.setLargeIcon(BitmapFactory.decodeResource(getBaseContext().getResources(), R.mipmap.ic_launcher))
.setColor(color)
.setContentTitle("New Message")
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(m, notBuilder.build());
}
}
This is my webview activity named ThirdActivity which receives the intent from notifications and starts the webview activity
public class ThirdActivity extends AppCompatActivity {
private WebView mWebView;
Context applicationContent = FirstActivity.getApplicationData();
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(applicationContent);
String link = mPrefs.getString("LINK","");
public class mWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new mWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl(link);
}
}
This is the crash report
> 07-01 14:05:57.101 2370-2423/com.sovraan E/AndroidRuntime: FATAL
> EXCEPTION: AsyncTask #1
> Process: com.sovraan, PID: 2370
> java.lang.NullPointerException: Attempt to invoke virtual method
> 'java.lang.String android.content.Context.getPackageName()' on a null
> object reference
> at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375)
> at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370)
> at com.sovraan.GCMPushReceiverService.onMessageReceived(GCMPushReceiverService.java:30)
> at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
> at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
> at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
> at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
> at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
> at java.lang.Thread.run(Thread.java:818)
One important thing to note
The crash happens only if the app is not running.
If I start the app before any notification is received then notifications are received, the app works fine. Again if I kill the app , the same crash happens when I receive notifications.