当我按下一个按钮时,下面的代码被调用。如果currentImageButton为null,并且Drawable为null(这是第一次调用),则只需将一条消息打印到控制台即可。否则,将画布的背景设置为Drawable文件夹中的图像。我知道条件有时是正确的,因为在第一次按下按钮后会打印出"currentImageButton DOES have a drawing cache"

            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;


但是,当条件为true时,永远不会设置画布的背景。奇怪的是,我知道这些代码行有效,因为当我将最后一行放在第一行之下时,一切工作都很好,尽管该行似乎与是否设置背景完全无关。此代码有效:

            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");

            }


这里发生了什么?

编辑

另外,另一件事很奇怪。如果我将条件更改为OR:

if (currentImageButton != null || currentImageButton.getDrawable() != 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)


编辑2

currentImageButton声明为全局变量,如下所示:

public ImageButton currentImageButton;


编辑3

我当前的实现。第一次单击该按钮,它按预期不起作用。第二次单击它通过了条件,但仍未设置画布的背景。

             Drawable d = imageButton.getDrawable();

            if (d != null) {
                Drawable myDrawable = getResources().getDrawable(R.drawable.start_checklist_pressed);
                Bitmap anImage      = ((BitmapDrawable) myDrawable).getBitmap();
                canvas.drawBitmap(anImage);

                System.out.println("worked");
            } else {
                System.out.println("didnt work");
            }

最佳答案

我在这里看到的问题是,您的currentImageButton可能已使用某种垃圾引用进行了初始化,因此不等于null变得有效,接下来您的currentImageButton.getDrawable()被调用,这实际上导致内部调用中出现NPE。在有效的代码中,在if条件之前有currentImageButton= imageButton

我相信这应该解决

public ImageButton currentImageButton=null;

10-06 02:21