0

我开发了一个绘画应用程序,我想插入裁剪功能,我使用 CanvasView 中的这段代码:

public void Crop() {
    // TODO Auto-generated method stub
    int value = 0;
    if (bitmap.getHeight() <= bitmap.getWidth()) {
        value = bitmap.getHeight();
    } else {
        value = bitmap.getWidth();
    }

    Bitmap finalBitmap = null;
    finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, value, value);
    Bitmap lastBitmap = null;
    lastBitmap = Bitmap.createScaledBitmap(finalBitmap, 144, 144, true);

}

但我收到以下错误

03-28 03:10:18.365: E/AndroidRuntime(19708): FATAL EXCEPTION: main
03-28 03:10:18.365: E/AndroidRuntime(19708): java.lang.NullPointerException
03-28 03:10:18.365: E/AndroidRuntime(19708):    at baked.soft.CanvasView.Crop(CanvasView.java:670)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at baked.soft.FirstActivity$7.onClick(FirstActivity.java:157)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at android.view.View.performClick(View.java:4232)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at android.view.View$PerformClick.run(View.java:17318)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at android.os.Handler.handleCallback(Handler.java:615)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at android.os.Looper.loop(Looper.java:137)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at android.app.ActivityThread.main(ActivityThread.java:4921)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at java.lang.reflect.Method.invokeNative(Native Method)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at java.lang.reflect.Method.invoke(Method.java:511)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-28 03:10:18.365: E/AndroidRuntime(19708):    at dalvik.system.NativeStart.main(Native Method)

这是有错误的行:

if (bitmap.getHeight() <= bitmap.getWidth()) {

来自 FirstActivity 的位图

imageViewCrop = (ImageView)this.findViewById(R.id.imageViewCrop);
    imageViewCrop.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Bitmap bitmap = Bitmap.createBitmap(myView.getWidth(),myView.getHeight(),Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            myView.draw(canvas);
            Toast.makeText(getApplicationContext(), "Redo tool clicked", Toast.LENGTH_SHORT).show();
            myView.Crop();  
            bitmap.recycle();
        }
    });
4

1 回答 1

2

输入bitmap为空,因此NullPoinerException.


顺便说一句,您的代码应该看起来更像这样:

public void Crop() {
    if(bitmap == null) {
        return;
    }

    final int value;
    if (bitmap.getHeight() <= bitmap.getWidth()) {
        value = bitmap.getHeight();
    } else {
        value = bitmap.getWidth();
    }

    final Bitmap finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, value, value);
    final Bitmap lastBitmap = Bitmap.createScaledBitmap(finalBitmap, 144, 144, true);
}
于 2013-03-28T01:20:47.100 回答