Android: Receiving NullPointerException when initialize a Canvas object

I have NullPointerException when the code gets to the canvas initialization. When creating the canvas I passed in a blank bitmap to it.

This is my code where the nullpointerexception occurs.

    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;

}

This is the stack trace of the code.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.PhotoEditActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) at 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) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4627) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.graphics.Canvas.(Canvas.java:82) at ImageManupulator.createS caledBitmap(ImageManupulator.java:101) at utils.ImageManupulator.ImageResizing(ImageManupulator.java:27) at android.PhotoEditActivity.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 more


在你调用这个方法的地方,请检查你的参数unscaledBitmap和scalingLogic不为null,因为这2个只是在你的方法实现中使用的对象。

链接地址: http://www.djcxy.com/p/31434.html

上一篇: 将项目加载到微调器时为空指针异常

下一篇: Android:初始化Canvas对象时接收NullPointerException