Jack
Jack

Reputation: 1855

How to add a notification with button?

I am a begginer, In my app I have a notification for image upload progress, I want to add a button with that notification, and I want to cancel the upload service when user clicks on the button. I have used the below code, I have created an xml layout for a button,

  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);

But when I run the app ,it throws a null pointer exception on this line

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

I don't understand how it happens,can anybody help?

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)

Upvotes: 0

Views: 100

Answers (1)

Blackbelt
Blackbelt

Reputation: 157487

But when I run the app ,it throws a null pointer exception on this line RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.activity_upload);

then ctx is not initialized, and you don't need it, since, ImageUploadActivity.this is already a context

Upvotes: 1

Related Questions