2013-05-25 71 views
-2

我試圖創建將在通知欄上顯示,並從開發者網站(http://developer.android.com/guide/topics/ui/notifiers/notifications.html) 複製的代碼的通知,但是當我打開應用程序崩潰。Android應用程序在此行崩潰:NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

下面的代碼:

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

後logcat輸出。 – Gustek

回答

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

刪除第一行末尾的分號。 所有這些調用都被鏈接並作爲一行執行,但爲了便於閱讀,還有新行。

你寫了應用程序崩潰,但這個錯字它甚至不應該編譯。 不編譯和崩潰是兩個非常不同的事情。

+0

對不起,我後來寫了分號。沒有分號但仍然崩潰, –