When I press a button, the code below is called. If currentImageButton is null, and it's Drawable is null (which it will be the first time it is called), then simply print a message to the console. Otherwise, set the background of my canvas to an image in my Drawable folder. I know the condition is sometimes true, because "currentImageButton DOES have a drawing cache" is printed out after the first button press.
ImageButton imageButton = (ImageButton) view;
if (currentImageButton != null && currentImageButton.getDrawable() != null) {
Drawable myDrawable = getResources().getDrawable(R.drawable.start_checklist_pressed);
Bitmap anImage = ((BitmapDrawable) myDrawable).getBitmap();
canvas.drawBitmap(anImage);
System.out.println("currentImageButton DOES have a drawing cache");
} else {
System.out.println("currentImageButton has no drawing cache");
}
currentImageButton = imageButton;
However, when the condition is true, the background of my canvas is never set. The weird thing is, I know those lines of code work, because when I bring the last line underneath the first, everything works perfectly, although that line seems completely irrelevant to whether or not the background gets set. This code works:
ImageButton imageButton = (ImageButton) view;
currentImageButton = imageButton;
if (currentImageButton != null && currentImageButton.getDrawable() != null) {
Drawable myDrawable = getResources().getDrawable(R.drawable.start_checklist_pressed);
Bitmap anImage = ((BitmapDrawable) myDrawable).getBitmap();
canvas.drawBitmap(anImage);
System.out.println("currentImageButton DOES have a drawing cache");
} else {
System.out.println("currentImageButton has no drawing cache");
}
What is going on here?
EDIT
Also, another weird thing. If I change the condition to OR:
if (currentImageButton != null || currentImageButton.getDrawable() != null)
I get a null pointer exception on this line. How is it possible for a null pointer exception to be thrown on a check for null?
java.lang.NullPointerException
at com.brettrosen.atls.fragments.PrearrivalPlan$6.onClick(PrearrivalPlan.java:416)
at android.view.View.performClick(View.java:4192)
at android.view.View$PerformClick.run(View.java:17327)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
EDIT2
currentImageButton is declared as follows, as a global variable:
public ImageButton currentImageButton;
Aucun commentaire:
Enregistrer un commentaire