gpt4 book ai didi

android - 打开通知时出现空指针异常

转载 作者:行者123 更新时间:2023-11-29 18:08:35 27 4
gpt4 key购买 nike

我正在 GCM 中为 Android 应用程序创建一些通知,我可以很好地创建通知,但是当我尝试从下拉栏打开它们时,我的应用程序无法打开。检查 LogCat 我可以看到如下回溯:

08-21 22:31:31.250: ERROR/ActivityManager(275): startLaunchActivity get appname failed
java.lang.NullPointerException
at com.android.server.am.ActivityStack.startLaunchActivity(ActivityStack.java:4654)
at com.android.server.am.ActivityStack.startActivityMayWait(ActivityStack.java:3196)
at com.android.server.am.ActivityManagerService.startActivityInPackage(ActivityManagerService.java:2628)
at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:231)
at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:182)
at android.content.IIntentSender$Stub.onTransact(IIntentSender.java:64)
at android.os.Binder.execTransact(Binder.java:338)
at dalvik.system.NativeStart.run(Native Method)
08-21 22:31:31.250: INFO/ActivityManager(275): START intent from pid -1

知道是什么原因造成的,我该如何解决?如果需要,我可以提供代码。

谢谢!

通知创建代码:

@Override
protected void onMessage(Context context, Intent intent) {
String msg = intent.getStringExtra(Constants.FIELD_MESSAGE);

Log.e("We got a message from the server",msg);

NotificationManager manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = prepareNotification(context, msg);
manager.notify(R.id.notification_id, notification);
Log.e("Sent notification event","");
}
private Notification prepareNotification(Context context, String msg) {
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.ic_stat_cloud, msg,
when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(context, MessageActivity.class);
// Set a unique data uri for each notification to make sure the activity
// gets updated
intent.setData(Uri.parse(msg));
intent.putExtra(Constants.FIELD_MESSAGE, msg);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
0);
String title = context.getString(R.string.app_name);
notification.setLatestEventInfo(context, title, msg, pendingIntent);

return notification;
}

通知处理代码:

public class MessageActivity extends Activity {
private TextView mMessageView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("opening message notification","");
setContentView(R.layout.activity_message);
Log.e("displaying message","");
mMessageView = (TextView) findViewById(R.id.message);
Log.e("put message","");
}

@Override
public void onResume() {
super.onResume();
String msg = getIntent().getStringExtra(Constants.FIELD_MESSAGE);
mMessageView.setText(msg);
}
}

最佳答案

从报错来看我怀疑是你忘了在Android list 文件中添加MessageActivity

关于android - 打开通知时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12063499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com