[英]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.