在获取任何推送通知时(而不是在每个设备中)有时会收到此错误 - 可能是设备问题? 最新在Android 5.0中崩溃了在GcmListenerService中获取错误
请帮忙!!
致命异常:显示java.lang.NullPointerException:尝试在android.os.Parcel上一个空 对象引用 调用 虚拟方法java.lang.String中java.lang.String.intern()' .readException(Parcel.java:1556) 在android.os.Parcel.readException(Parcel.java:1499) 在android.app.ActivityManagerProxy.getIntentSender(ActivityManagerNative.java:3740) 在android.app.PendingIntent.getActivity (PendingIntent.java:291) at android.app.PendingIntent.getActivity(PendingIntent.java:252) at com.mystuc.Service.MyGcmListenerService.sendNotification(MyGcmListenerServi ce.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(未知来源) 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:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker。运行(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());
}}
请说明您的问题@Gurvinder –
我已经加入一些更多的细节,请检查。谢谢 –