简体   繁体   English

如何在我的 Android 应用程序中修复此错误?

[英]How can I fix this error in my Android app?

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 04-04 16:10:27.445 25895-25895/com.blackswan.shaghayeghaccounting E/AndroidRuntime:致命异常:主进程:com.blackswan.shaghayeghaccounting,PID:25895 java.langable.runtimeException {com.blackswan.shaghayeghaccounting} .shaghayeghaccounting/com.blackswan.shaghayeghaccounting.transactions.TransparentQuickActionActivity}:java.lang.NullPointerException:尝试在 android.app.ActivityThread.performResumeActivity 的空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()” (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) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:148)在 android.app.ActivityThread.main(ActivityThread.java:7325) 在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) 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) 引起:java.lang.NullPointerException:尝试在 android.graphics.Bitmap.createBitmap(Bitmap.java:771) 处的空对象引用上调用虚拟方法“int android.graphics.Bitmap.getWidth()”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复我的 android 应用程序中的布局视图? - How can I fix the layout views in my android app? 我如何在我的应用程序上修复“Button()。isEnabled”错误? - How can i fix“ Button().isEnabled” error on my app? 如何修复FirebaseAuthWebException应用程式Android错误? - How I can fix error firebaseAuthWebException app android? 如何修复 Android 应用启动停止错误? - How can I fix Android app launch stopped error? 如何将我的 android 应用下载到我的电脑上,以便我可以在 Android Studio 中修复它 - How to download my android app to my computer so I can fix it in Android Studio 如何修复损坏的应用程序? - How can I fix my corrupt app? 当我使用 TabLayout 时,我的 Android Studio 的应用程序崩溃了,我该如何解决? - My Android Studio's app collapses when I use TabLayout, how can I fix it? 我该如何修复Android中的AnalyticsTrackers错误 - How can i fix AnalyticsTrackers error in Android 每当我尝试在 Android Studio 3.6 上运行应用程序时。 它带来了这个错误......我该如何解决这个问题,以便我的应用程序可以在模拟器中运行 - whenever i try running an app on Android studio 3.6. it brings this error... how can i fix this so that my apps can run in an emulator 我该如何解决这个Android错误? - How can i fix this android error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM