Android:初始化Canvas对象时接收NullPointerException
当代码进入画布初始化时,我有NullPointerException。 在创建画布时,我将空白位图传递给它。
这是我的代码发生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:无法启动活动ComponentInfo {com.android.PhotoEditActivity}:显示java.lang.NullPointerException在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)在android.app.ActivityThread.handleLaunchActivity(ActivityThread。 Java的:2679)在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)在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(本机方法)在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.createS caledBitmap(ImageManupulator.java:101)在utils.ImageManupulator.ImageResizing(ImageManupulator.java:27)在android.PhotoEditActivity.updatePhoto(PhotoEditActivity.java:219)在android.PhotoEditActivity.access $ 0(PhotoEditActivity.java:211)在安卓.PhotoEditActivity $ 2.onItemClick(PhotoEditActivity.java:279)在android.widget.AdapterView.performItemClick(AdapterView.java:284)在android.PhotoEditActivity.onCreate(PhotoEditActivity.java:128)在android.app.Instrumentation.callActivityOnCreate(仪表.java:1047)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)... 11 more
在你调用这个方法的地方,请检查你的参数unscaledBitmap和scalingLogic不为null,因为这2个只是在你的方法实现中使用的对象。
链接地址: http://www.djcxy.com/p/31433.html上一篇: Android: Receiving NullPointerException when initialize a Canvas object