App crashes on received gcm message when it's closed

可紊 提交于 2020-01-07 07:13:46

问题


When my app is closed (i.e swiped off by the user from the App Switching menu), it crashes in onMessageReceived().

Here's onMessageReceived():

@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    from = data.getString("sender");

    File notificationsFile = Constants.getNotificationsFlagFile();
    ...
}

It crashes on that last line, giving a null pointer exception:

08-13 21:15:25.535 23201-23216/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1 java.lang.ExceptionInInitializerError at com.example.myapp.chatGCM.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:52) at com.google.android.gms.gcm.GcmListenerService.zzs(Unknown Source) at com.google.android.gms.gcm.GcmListenerService.zzk(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:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:841) Caused by: java.lang.NullPointerException at helpers.Constants.(Constants.java:54)             at com.example.myapp.chatGCM.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:52)             at com.google.android.gms.gcm.GcmListenerService.zzs(Unknown Source)             at com.google.android.gms.gcm.GcmListenerService.zzk(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:1080)             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)             at java.lang.Thread.run(Thread.java:841)

and here's getNotificationsFlagFile():

public static File getNotificationsFlagFile()
{
    return new File(MainApplication.getAppContext().getFilesDir(), "notifications_"+getSession());
}

and finally:

private static String getSession()
{
    File file = new File(MainApplication.getAppContext().getFilesDir(), "session");
    int length = (int) file.length();
    byte[] bytes = new byte[length];
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        try {
            in.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return (new String(bytes));
}

All I'm using is static variables and functions. Why is this happening?


回答1:


From here you seem to be having an un-expected behavior... Probably from the getAppContext() function... try using getApplicationContext() instead.



来源:https://stackoverflow.com/questions/31995818/app-crashes-on-received-gcm-message-when-its-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!