-1

I designed a widget for my application that worked already, but now when I click on the widget, I encounter the following error.Error occurred

My code

@Override
    protected void onResume() {
        super.onResume();

        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());

        //ByteArrayOutputStream out = new ByteArrayOutputStream();
        //bitmap.compress(Bitmap.CompressFormat.PNG,90,out);

        v1.setDrawingCacheEnabled(false);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(bitmap)
                .setContentTitle("title")
                .setContentText("message:")
                .setAutoCancel(true)
                .setOngoing(false)
                .setTicker("ticker");

        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(123456, builder.build());
    }

Error occurred:

  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
    at android.graphics.Bitmap.createBitmap(Bitmap.java:771)
    at com.blackswan.shaghayeghaccounting.transactions.TransparentQuickActionActivity.onResume(TransparentQuickActionActivity.java:113)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1287)
    at android.app.Activity.performResume(Activity.java:7015)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4210)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4323) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3426) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:7325) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

04-04 16:10:27.445 25895-25895/com.blackswan.shaghayeghaccounting E/AndroidRuntime: FATAL EXCEPTION: main Process: com.blackswan.shaghayeghaccounting, PID: 25895 java.lang.RuntimeException: Unable to resume activity {com.blackswan.shaghayeghaccounting/com.blackswan.shaghayeghaccounting.transactions.TransparentQuickActionActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4221) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4323) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3426) at android.app.ActivityThread.access$1100(ActivityThread.java:229) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.graphics.Bitmap.createBitmap(Bitmap.java:771)

  • Maybe the value of `bitmap` is null. Try to put this code to `onCreate` method. – Faysal Ahmed Apr 04 '19 at 11:52
  • You are running the method `v1.getDrawingCache()` too soon. The view itself is not yet drawn and thus it returns null. You can see a good answere here: https://stackoverflow.com/a/4618030/678448 Putting the code into onCreate is a bad idea since onCreate is called ahead of onResume. – Thommy Apr 04 '19 at 11:54
  • 4
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Apr 04 '19 at 11:57
  • https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/218510#218510 check this – Robert LaFondue Apr 04 '19 at 12:00

1 Answers1

0

Try This:

View viewBitmap = getWindow().getDecorView().getRootView();
viewBitmap.setDrawingCacheEnabled(true);
viewBitmap.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),     
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
viewBitmap.layout(0, 0, viewBitmap .getMeasuredWidth(), viewBitmap.getMeasuredHeight()); 
viewBitmap.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(viewBitmap .getDrawingCache());
viewBitmap.setDrawingCacheEnabled(false); 
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49