0

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)
Pankaj
  • 7,908
  • 6
  • 42
  • 65
Jack
  • 1,825
  • 3
  • 26
  • 43

1 Answers1

1

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

Blackbelt
  • 156,034
  • 29
  • 297
  • 305