首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打开通知时出现空指针异常

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

Stack Overflow用户
提问于 2012-08-22 05:43:26
回答 1查看 1.5K关注 0票数 1

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

代码语言:javascript
复制
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

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

谢谢!

通知创建代码:

代码语言:javascript
复制
@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;
}

通知处理代码:

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-22 06:13:36

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12063499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档