应用程序在关闭时因收到 gcm 消息而崩溃
App crashes on received gcm message when it's closed
当我的应用程序关闭时(即用户从“应用程序切换”菜单中关闭),它会在 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﹕
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.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.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)
这里是 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));
}
我使用的只是静态变量和函数。为什么会这样?
From here 你似乎有一个意想不到的行为......可能来自 getAppContext()
函数......尝试改用 getApplicationContext()
。
当我的应用程序关闭时(即用户从“应用程序切换”菜单中关闭),它会在 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﹕ 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.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.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)
这里是 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));
}
我使用的只是静态变量和函数。为什么会这样?
From here 你似乎有一个意想不到的行为......可能来自 getAppContext()
函数......尝试改用 getApplicationContext()
。