当代码进入画布初始化时,我有 NullPointerException。创建画布时,我将一个空白位图传递给它。
这是我发生空指针异常的代码。
private static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
这是代码的堆栈跟踪。
java.lang.RuntimeException: 无法在 android.app.ActivityThread.handleLaunchActivity(ActivityThread. java:2679) at android.app.ActivityThread.access$2300(ActivityThread.java:125) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) at android.os.Handler.dispatchMessage(Handler.java: 99) 在 android.os.Looper.loop(Looper.java:123) 在 android.app.ActivityThread.main(ActivityThread.java:4627) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang .reflect.Method.invoke(Method.java:521) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java :616) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.NullPointerException at android.graphics.Canvas.(Canvas.java:82) at ImageManupulator.createScaledBitmap(ImageManupulator.java:101) at utils。 ImageManupulator.ImageResizing(ImageManupulator.java:27) 在 android.PhotoEditActivity.updatePhoto(PhotoEditActivity.java:219) 在 android.PhotoEditActivity.access$0(PhotoEditActivity.java:211) 在 android.PhotoEditActivity$2.onItemClick(PhotoEditActivity.java:279) ) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 在 android.app.ActivityThread 的 android.widget.AdapterView.performItemClick(AdapterView.java:284) 在 android.PhotoEditActivity.onCreate(PhotoEditActivity.java:128) .performLaunchActivity(ActivityThread.java:2627) ... 11 更多主要(本机方法)引起:java.lang.NullPointerException at android.graphics.Canvas.(Canvas.java:82) at ImageManupulator.createScaledBitmap(ImageManupulator.java:101) at utils.ImageManupulator.ImageResizing(ImageManupulator.java:27 ) 在 android.PhotoEditActivity.updatePhoto(PhotoEditActivity.java:219) 在 android.PhotoEditActivity.access$0(PhotoEditActivity.java:211) 在 android.PhotoEditActivity$2.onItemClick(PhotoEditActivity.java:279) 在 android.widget.AdapterView.performItemClick (AdapterView.java:284) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 在 android.PhotoEditActivity.onCreate(PhotoEditActivity.java:128) ... 11 更多主要(本机方法)引起:java.lang.NullPointerException at android.graphics.Canvas.(Canvas.java:82) at ImageManupulator.createScaledBitmap(ImageManupulator.java:101) at utils.ImageManupulator.ImageResizing(ImageManupulator.java:27 ) 在 android.PhotoEditActivity.updatePhoto(PhotoEditActivity.java:219) 在 android.PhotoEditActivity.access$0(PhotoEditActivity.java:211) 在 android.PhotoEditActivity$2.onItemClick(PhotoEditActivity.java:279) 在 android.widget.AdapterView.performItemClick (AdapterView.java:284) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 在 android.PhotoEditActivity.onCreate(PhotoEditActivity.java:128) ... 11 更多82) 在 ImageManupulator.createScaledBitmap(ImageManupulator.java:101) 在 utils.ImageManupulator.ImageResizing(ImageManupulator.java:27) 在 android.PhotoEditActivity.updatePhoto(PhotoEditActivity.java:219) 在 android.PhotoEditActivity.access$0(PhotoEditActivity.java :211) 在 android.PhotoEditActivity$2.onItemClick(PhotoEditActivity.java:279) 在 android.widget.AdapterView.performItemClick(AdapterView.java:284) 在 android.PhotoEditActivity.onCreate(PhotoEditActivity.java:128) 在 android.app。 Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) ... 11 更多82) 在 ImageManupulator.createScaledBitmap(ImageManupulator.java:101) 在 utils.ImageManupulator.ImageResizing(ImageManupulator.java:27) 在 android.PhotoEditActivity.updatePhoto(PhotoEditActivity.java:219) 在 android.PhotoEditActivity.access$0(PhotoEditActivity.java :211) 在 android.PhotoEditActivity$2.onItemClick(PhotoEditActivity.java:279) 在 android.widget.AdapterView.performItemClick(AdapterView.java:284) 在 android.PhotoEditActivity.onCreate(PhotoEditActivity.java:128) 在 android.app。 Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) ... 11 更多updatePhoto(PhotoEditActivity.java:219) at android.PhotoEditActivity.access$0(PhotoEditActivity.java:211) at android.PhotoEditActivity$2.onItemClick(PhotoEditActivity.java:279) at android.widget.AdapterView.performItemClick(AdapterView.java:284 ) at android.PhotoEditActivity.onCreate(PhotoEditActivity.java:128) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) ... 11 更多updatePhoto(PhotoEditActivity.java:219) at android.PhotoEditActivity.access$0(PhotoEditActivity.java:211) at android.PhotoEditActivity$2.onItemClick(PhotoEditActivity.java:279) at android.widget.AdapterView.performItemClick(AdapterView.java:284 ) at android.PhotoEditActivity.onCreate(PhotoEditActivity.java:128) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) ... 11 更多1047) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) ... 还有 11 个1047) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) ... 还有 11 个