java.lang.NullPointerException:尝试调用虚拟方法' int android.graphics.Bitmap.getHeight()'在null对象引用上

时间:2016-08-24 18:13:55

标签: java android bitmap

我是刚开始学习Android的学生。今天我试图使用位图剪切图像,但只是为了得到这个:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getHeight()' on a null object reference

以下是相关的代码。他们实际上非常简单:

 Matrix matrix = new Matrix();
 Bitmap original =  BitmapFactory.decodeResource(getResources(),imageButtonID[3]);
 int height = 36, width = 44;
 int orgHeight = original.getHeight();
 int orgWidth = original.getWidth();
 float scaleWidth = ((float) width) / orgWidth;
 float scaleHeight = ((float) height) / orgHeight;
 matrix.setScale(scaleWidth, scaleHeight);
 Bitmap bitmap = Bitmap.createBitmap(original, 0 , 0 , width, height, matrix, true);
 imageButtons[3].setImageBitmap(original);

堆栈跟踪:

java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.einsambr.bridge_0_0_0/com.einsambr.bridge_0_0_0.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getHeight()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getHeight()' on a null object reference
    at com.einsambr.bridge_0_0_0.MainActivity.onCreate(MainActivity.java:30)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我很困惑。我想这里的问题必须是Bitmap original有一个null对象引用,但为什么它指向null?有人可以帮我解决吗?非常感谢!

0 个答案:

没有答案