Android应用程序在此行崩溃:NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

时间:2013-05-25 22:32:28

标签: android notifications

我正在尝试创建一个通知,该通知将显示在通知栏中并从开发者网站(http://developer.android.com/guide/topics/ui/notifiers/notifications.html)复制代码 但是当我打开应用程序时它会崩溃。

这是代码:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(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(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(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(3,mBuilder.build());

我发现这一行

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

导致崩溃。

1 个答案:

答案 0 :(得分:0)

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!");

在第一行末尾删除分号。 所有这些调用都被链接并作为一行执行,只有可读性的新行。

你写了应用程序崩溃,但有这个错字它甚至不应该编译。 不编译和崩溃是两回事。