2

当用户点击通知时如何启动活动?我经常崩溃。我无法点击通知工作。下面显示了一个 onclick 方法。当按下一个名为 ButtonOne 的按钮时,它将在屏幕的顶部菜单栏中启动一个通知。我希望用户能够按下通知并让它启动名为 MainActivity 的活动。它崩溃并且不会启动页面。我放在 MainActivity 类中的通知代码可能有问题。怎么了?

    ButtonOne.setOnClickListener(new View.OnClickListener() {

      private int mId;

    // anonymous inner class override for on click
    public void onClick(View v) {

        Intent myIntent = new Intent(MainActivity.this, CoverFlowExample.class);
         MainActivity.this.startActivity(myIntent);



         NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(MainActivity.this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(MainActivity.this, MainActivity.class);

            // The stack builder object will contain an artificial back stack for the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                    );
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // mId allows you to update the notification later on.
            mNotificationManager.notify(mId, mBuilder.build());


    }
});
4

1 回答 1

2

发现问题出在 AVD android 模拟器而不是代码。由于某种原因,它没有更新早期版本的代码。再次测试它,现在它运行没有错误。

于 2012-10-18T07:09:55.710 回答