如何使用按钮添加通知?

时间:2015-07-17 10:01:26

标签: android android-notifications android-remoteview

我是初学者,在我的应用中,我有通知图片上传进度,我想添加button 通知,我想要在用户点击button时取消上传服务。我使用了以下代码,我为xm创建了button l布局,

  mBuilder = new android.support.v4.app.NotificationCompat.Builder(ImageUploadActivity.this);
         mBuilder.setContentTitle("Cookbook Upload")
            .setContentText("Upload in Progress")
            .setTicker("Cookbook Upload")
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_launcher)
            .build();
    RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.activity_upload);

但是当我运行应用程序时,它会在此行引发空指针异常

 RemoteViews contentView=new RemoteViews(ctx.getPackageName(),     R.layout.activity_upload);

我不明白它是如何发生的,有人可以帮忙吗?

logcat的

07-17 15:06:34.733  21652-21879/? E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[MyIntentService]
java.lang.NullPointerException
        at project1.jbn.com.cookbookintent.ImageUploadActivity.onHandleIntent(ImageUploadActivity.java:67)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)

1 个答案:

答案 0 :(得分:1)

  

但是当我运行应用程序时,它会抛出一个空指针异常   line RemoteViews contentView = new RemoteViews(ctx.getPackageName(),R.layout.activity_upload);

然后ctx未初始化,您不需要它,因为ImageUploadActivity.this已经是上下文