用于关闭对话框的按钮会抛出nullpointerexception

时间:2014-06-11 23:56:57

标签: java android button dialog nullpointerexception

我有一个会显示图像的对话框。我在对话框中有一个按钮,它应该关闭对话框,因此它是一个退出按钮。按下它时,我得到一个空指针异常。

以下是包含对话框的代码:

    buildMoreButton = (Button) findViewById(R.id.buildMore);
    buildMoreButton.setText("BUILD");
    buildMoreButton.setBackgroundColor(-65536);
    buildMoreButton.setOnTouchListener(new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                buildMoreButton.setBackgroundColor(-1);
            }//end if
            else if (event.getAction() == MotionEvent.ACTION_UP)
            {
                buildMoreButton.setBackgroundColor(-65536);
            }//end else if

            //open dialog for building choices
            buildingSelect = new Dialog(runGraphics.this);
            buildingSelect.setContentView(R.layout.dialog);
            buildingSelect.setTitle("Building Selection");

            exit = (Button) findViewById(R.id.exit);
            exit.setText("X");
            exit.setBackgroundColor(-65536);
            exit.setOnTouchListener(new OnTouchListener()
            {
                @Override
                public boolean onTouch(View v, MotionEvent event) 
                {
                    if (event.getAction() == MotionEvent.ACTION_DOWN)
                    {
                        exit.setBackgroundColor(-1);
                    }//end if
                    else if (event.getAction() == MotionEvent.ACTION_UP)
                    {
                        exit.setBackgroundColor(-65536);
                    }//end else if

                    buildingSelect.dismiss();
                    return false;
                }//end onTouch          
            });//end OnTouchListener

            colonyHut = (ImageView) findViewById(R.id.colonyHut);

            buildingSelect.show();
            return false;
        }//end onTouch function

    });//end OnTouchListener

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<ImageView 
    android:id="@+id/colonyHut" 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:src="@drawable/mainhut" />

<Button
    android:id="@+id/exit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_gravity="top|right" />
</LinearLayout>

这是我的logcat:

06-11 18:50:29.228: E/AndroidRuntime(28480): FATAL EXCEPTION: main
06-11 18:50:29.228: E/AndroidRuntime(28480): java.lang.NullPointerException
06-11 18:50:29.228: E/AndroidRuntime(28480):    at com.project.llb.runGraphics$6.onTouch(runGraphics.java:329)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.view.View.dispatchTouchEvent(View.java:3881)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1923)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1190)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.app.Activity.dispatchTouchEvent(Activity.java:2155)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1907)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2197)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1881)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.os.Looper.loop(Looper.java:130)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at android.app.ActivityThread.main(ActivityThread.java:3806)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at java.lang.reflect.Method.invoke(Method.java:507)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-11 18:50:29.228: E/AndroidRuntime(28480):    at dalvik.system.NativeStart.main(Native Method)

提前感谢帮助人员。

1 个答案:

答案 0 :(得分:1)

你得到了错误的视图参考,从而为你提供 NPE ,你正在使用findViewById,它会在你的对话框布局中找到你的活动布局视图,而不是调用{获取视图的findViewById {1}}

<强>溶液

Dialog