【问题标题】:App crashes on received gcm message when it's closed应用程序在关闭时收到 gcm 消息时崩溃
【发布时间】:2015-08-13 18:28:31
【问题描述】:

当我的应用关闭(即用户从“应用切换”菜单中滑动关闭)时,它会在 onMessageReceived() 中崩溃。

这里是onMessageReceived()

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

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

它在最后一行崩溃,给出null pointer exception

08-13 21:15:25.535 23201-23216/com.example.myapp E/AndroidRuntime﹕ 致命异常:AsyncTask #1 java.lang.ExceptionInInitializerError 在 com.example.myapp.chatGCM.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:52) 在 com.google.android.gms.gcm.GcmListenerService.zzs(未知来源) 在 com.google.android.gms.gcm.GcmListenerService.zzk(未知来源) 在 com.google.android.gms.gcm.GcmListenerService.zza(未知来源) 在 com.google.android.gms.gcm.GcmListenerService$1.run(未知来源) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 在 java.lang.Thread.run(Thread.java:841) 引起:java.lang.NullPointerException 在 helpers.Constants.(Constants.java:54) 在 com.example.myapp.chatGCM.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:52) 在 com.google.android.gms.gcm.GcmListenerService.zzs(未知来源) 在 com.google.android.gms.gcm.GcmListenerService.zzk(未知来源) 在 com.google.android.gms.gcm.GcmListenerService.zza(未知来源) 在 com.google.android.gms.gcm.GcmListenerService$1.run(未知来源) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 在 java.lang.Thread.run(Thread.java:841)

这里是getNotificationsFlagFile()

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

最后:

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

我使用的是静态变量和函数。为什么会这样?

【问题讨论】:

    标签: android android-asynctask google-cloud-messaging


    【解决方案1】:

    From here 你似乎有意外的行为...可能来自getAppContext() 函数...尝试改用getApplicationContext()

    【讨论】:

    • 你说得对。 getAppContext()MainApplication 中的静态方法,它返回 MainApplication.context; ,在 MainApplicationonCreate() 中初始化。由于应用关闭,它还没有被“创建”。
    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 2013-09-28
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多