在 GcmListenerService 中获取错误
Getting error in GcmListenerService
有时在收到任何推送通知时出现此错误(并非在每台设备中)——这可能是设备问题??
最近在 Android 5.0
中崩溃
请帮忙!!
Fatal Exception: java.lang.NullPointerException: Attempt to invoke
virtual method 'java.lang.String java.lang.String.intern()' on a null
object reference
at android.os.Parcel.readException(Parcel.java:1556)
at android.os.Parcel.readException(Parcel.java:1499)
at android.app.ActivityManagerProxy.getIntentSender(ActivityManagerNative.java:3740)
at android.app.PendingIntent.getActivity(PendingIntent.java:291)
at android.app.PendingIntent.getActivity(PendingIntent.java:252)
at com.mystuc.Service.MyGcmListenerService.sendNotification(MyGcmListenerService.java:122)
at com.mystuc.Service.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:82)
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.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
我正在使用这个代码:
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
private Intent intent;
@Override
public void onMessageReceived(String from, Bundle data) {
Log.e("onMessageReceived", "onMessageReceived:" + data.toString());
Utils pref = Utils.getInstance(getApplicationContext());
String uid = pref.getString(Constants.uid, "");
if (uid.length() > 0) {
if (pref.getBoolean(Constants.isEnableNotification, false)) {
try {
String response = data.getString("data");
if (response != null) {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.has("stype")) {
String stype = jsonObject.getString("stype");
if (stype.equalsIgnoreCase("timeline")) {
String msg = jsonObject.getString("msg");
String post_type = jsonObject.getString("post_type");
String post_id;
String postDetails = "";
if (post_type.equalsIgnoreCase(Constants.PostType.Question)) {
post_id = jsonObject.getString("question_id");
} else {
post_id = jsonObject.getString("post_id");
try {
postDetails = jsonObject.getJSONArray("postDetail").getJSONObject(0).toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
String post_tab = jsonObject.getString("post_tab");
String message = msg.trim();
if (post_type.equalsIgnoreCase(Constants.PostType.Question)) {
intent = new Intent(getApplicationContext(), QuestionView.class);
} else {
intent = new Intent(getApplicationContext(), FeedDetail_home.class);
}
intent.putExtra(Constants.fromTimeLineNotification, true);
intent.putExtra(Constants.posttype, post_type);
intent.putExtra(Constants.postid, post_id);
intent.putExtra(Constants.posttab, post_tab);
intent.putExtra(Constants.postDetailsJson, postDetails);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
sendNotification(message, intent);
}
} else {
String msg = jsonObject.getString("msg");
String sender = jsonObject.getString("sent_by");
String templeteid = jsonObject.getString("template_id");
String message = msg.trim();
Intent intent = new Intent(this, Home.class);
intent.putExtra(Constants.fromNotification, true);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
sendNotification(message, intent);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
sendBroadcast();
}
public void sendBroadcast() {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Constants.OnNotification));
}
private void sendNotification(String message, Intent intent) {
Bitmap icon = BitmapFactory.decodeResource(
MyGcmListenerService.this.getResources(),
R.drawable.app_icon);
final Intent notificationIntent = new Intent(this, Home.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
int color = getResources().getColor(R.color.colorPrimary);
Uri sound = Uri.parse("android.resource://com.mystuc/raw/coindrop");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_smallnotify)
.setContentTitle("MyStuC")
.setContentText(message)
.setAutoCancel(true)
.setColor(color)
.setSound(sound)
.setLargeIcon(icon)
.setContentIntent(pendingIntent).setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}}
int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
而不是给 0,
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
给那个 uniqueint
有时在收到任何推送通知时出现此错误(并非在每台设备中)——这可能是设备问题?? 最近在 Android 5.0
中崩溃请帮忙!!
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.intern()' on a null object reference at android.os.Parcel.readException(Parcel.java:1556) at android.os.Parcel.readException(Parcel.java:1499) at android.app.ActivityManagerProxy.getIntentSender(ActivityManagerNative.java:3740) at android.app.PendingIntent.getActivity(PendingIntent.java:291) at android.app.PendingIntent.getActivity(PendingIntent.java:252) at com.mystuc.Service.MyGcmListenerService.sendNotification(MyGcmListenerService.java:122) at com.mystuc.Service.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:82) 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.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
我正在使用这个代码:
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
private Intent intent;
@Override
public void onMessageReceived(String from, Bundle data) {
Log.e("onMessageReceived", "onMessageReceived:" + data.toString());
Utils pref = Utils.getInstance(getApplicationContext());
String uid = pref.getString(Constants.uid, "");
if (uid.length() > 0) {
if (pref.getBoolean(Constants.isEnableNotification, false)) {
try {
String response = data.getString("data");
if (response != null) {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.has("stype")) {
String stype = jsonObject.getString("stype");
if (stype.equalsIgnoreCase("timeline")) {
String msg = jsonObject.getString("msg");
String post_type = jsonObject.getString("post_type");
String post_id;
String postDetails = "";
if (post_type.equalsIgnoreCase(Constants.PostType.Question)) {
post_id = jsonObject.getString("question_id");
} else {
post_id = jsonObject.getString("post_id");
try {
postDetails = jsonObject.getJSONArray("postDetail").getJSONObject(0).toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
String post_tab = jsonObject.getString("post_tab");
String message = msg.trim();
if (post_type.equalsIgnoreCase(Constants.PostType.Question)) {
intent = new Intent(getApplicationContext(), QuestionView.class);
} else {
intent = new Intent(getApplicationContext(), FeedDetail_home.class);
}
intent.putExtra(Constants.fromTimeLineNotification, true);
intent.putExtra(Constants.posttype, post_type);
intent.putExtra(Constants.postid, post_id);
intent.putExtra(Constants.posttab, post_tab);
intent.putExtra(Constants.postDetailsJson, postDetails);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
sendNotification(message, intent);
}
} else {
String msg = jsonObject.getString("msg");
String sender = jsonObject.getString("sent_by");
String templeteid = jsonObject.getString("template_id");
String message = msg.trim();
Intent intent = new Intent(this, Home.class);
intent.putExtra(Constants.fromNotification, true);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
sendNotification(message, intent);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
sendBroadcast();
}
public void sendBroadcast() {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Constants.OnNotification));
}
private void sendNotification(String message, Intent intent) {
Bitmap icon = BitmapFactory.decodeResource(
MyGcmListenerService.this.getResources(),
R.drawable.app_icon);
final Intent notificationIntent = new Intent(this, Home.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
int color = getResources().getColor(R.color.colorPrimary);
Uri sound = Uri.parse("android.resource://com.mystuc/raw/coindrop");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_smallnotify)
.setContentTitle("MyStuC")
.setContentText(message)
.setAutoCancel(true)
.setColor(color)
.setSound(sound)
.setLargeIcon(icon)
.setContentIntent(pendingIntent).setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}}
int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
而不是给 0,
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
给那个 uniqueint