使用" NotificationManager"致命异常

时间:2014-04-13 18:47:15

标签: android notifications android-notifications

我正在尝试按照Android tuto网站实施示例通知管理器应用程序。

执行后如果我点击开始通知按钮,我就会低于例外

04-13 18:40:30.965: E/AndroidRuntime(2374): FATAL EXCEPTION: main
04-13 18:40:30.965: E/AndroidRuntime(2374): java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.example.notificationmanager/com.example.notificationmanager.NotificationView}
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:180)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:151)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at com.example.notificationmanager.MainActivity.displayNotification(MainActivity.java:64)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at com.example.notificationmanager.MainActivity$1.onClick(MainActivity.java:27)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.view.View.performClick(View.java:4204)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.view.View$PerformClick.run(View.java:17355)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.os.Handler.handleCallback(Handler.java:725)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.os.Looper.loop(Looper.java:137)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at java.lang.reflect.Method.invoke(Method.java:511)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at dalvik.system.NativeStart.main(Native Method)
04-13 18:40:30.965: E/AndroidRuntime(2374): Caused by: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.example.notificationmanager/com.example.notificationmanager.NotificationView}
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:225)
04-13 18:40:30.965: E/AndroidRuntime(2374):     at android.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:167)
04-13 18:40:30.965: E/AndroidRuntime(2374):     ... 14 more

MainActivity.java

package com.example.notificationmanager;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
   private NotificationManager mNotificationManager;
   private int notificationID = 100;
   private int numMessages = 0;

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      Button startBtn = (Button) findViewById(R.id.start);
      startBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            displayNotification();
         }
      });

      Button cancelBtn = (Button) findViewById(R.id.cancel);
      cancelBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            cancelNotification();
         }
      });

      Button updateBtn = (Button) findViewById(R.id.update);
      updateBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            updateNotification();
         }
      });
   }
   protected void displayNotification() {
      Log.i("Start", "notification");

      /* Invoking the default notification service */
      NotificationCompat.Builder  mBuilder = 
      new NotificationCompat.Builder(this); 

      mBuilder.setContentTitle("New Message");
      mBuilder.setContentText("You've received new message.");
      mBuilder.setTicker("New Message Alert!");
      mBuilder.setSmallIcon(R.drawable.ganesh);

      /* Increase notification number every time a new notification arrives */
      mBuilder.setNumber(++numMessages);

      /* Creates an explicit intent for an Activity in your app */
      Intent resultIntent = new Intent(this, NotificationView.class);

      TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
      stackBuilder.addParentStack(NotificationView.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);

      mNotificationManager =
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

      /* notificationID allows you to update the notification later on. */
      mNotificationManager.notify(notificationID, mBuilder.build());
   }

   protected void cancelNotification() {
      Log.i("Cancel", "notification");
      mNotificationManager.cancel(notificationID);
   }

   protected void updateNotification() {
      Log.i("Update", "notification");

      /* Invoking the default notification service */
      NotificationCompat.Builder  mBuilder = 
      new NotificationCompat.Builder(this); 

      mBuilder.setContentTitle("Updated Message");
      mBuilder.setContentText("You've got updated message.");
      mBuilder.setTicker("Updated Message Alert!");
      mBuilder.setSmallIcon(R.drawable.ganesh);

     /* Increase notification number every time a new notification arrives */
      mBuilder.setNumber(++numMessages);

      /* Creates an explicit intent for an Activity in your app */
      Intent resultIntent = new Intent(this, NotificationView.class);

      TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
      stackBuilder.addParentStack(NotificationView.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);

      mNotificationManager =
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

      /* Update the existing notification using same notification ID */
      mNotificationManager.notify(notificationID, mBuilder.build());
   }
}

0 个答案:

没有答案