Android通知崩溃了我的应用

时间:2016-07-01 08:26:56

标签: android webview notifications

我有一个Android应用程序在收到通知时崩溃。通知从我的应用服务器接收消息。更改通知会打开webview活动。我的问题是当我在webview中硬编码要打开的URL链接时,它工作正常。但是,当我使用从我的应用服务器发送的URL链接(字符串值)并将其保存在共享首选项中,然后使用它来启动webview活动时,应用程序崩溃。

代码如下: -

这是GCMPushReceiverClass,我收到消息并发送通知: -

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());
      }
}

这是我的名为ThirdActivity的webview活动,它通过通知接收意图并启动webview活动

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);
     }
}

这是崩溃报告

> 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)

需要注意的一件重要事情

只有在应用未运行时才会发生崩溃。

如果我在收到任何通知之前启动应用程序然后收到通知,该应用程序正常工作。再次,如果我杀了应用程序,我收到通知时会发生同样的崩溃。

1 个答案:

答案 0 :(得分:0)

这似乎是一个背景问题

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at ...

静态上下文对我来说似乎有风险:/上下文是实时发展它可以改变状态的东西。

你说只有当应用程序没有运行时才会发生崩溃,所以服务会收到来自GSM的消息,但是你获取上下文的活动没有初始化它

尝试在您需要时从GCMPushReceiverService访问上下文