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

时间:2012-08-21 21:43:26

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

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

1 个答案:

答案 0 :(得分:3)

从错误判断我怀疑您忘记将MessageActivity添加到Android清单文件