【问题标题】:Button used to close dialog throws nullpointerexception用于关闭对话框的按钮引发 nullpointerexception
【发布时间】:2014-06-11 23:56:57
【问题描述】:

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

这是我的包含对话框的代码:

    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>

这是我的日志:

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)

提前感谢各位的帮助。

【问题讨论】:

  • runGraphics.java:329 中有什么?
  • 对话框的代码与 runGraphics 相关。第 329 行是行 exit.setText("x");
  • runGraphics 是一项活动吗??
  • 是的,这是一项活动..
  • 那么runGraphics的第329行是什么代码?

标签: java android button dialog nullpointerexception


【解决方案1】:

你得到了错误的 View 引用,因此给你 NPE 你正在使用findViewById,它会发现你的活动布局的 View 不在你的对话框布局中,而是调用 findViewById Dialog 获取视图

解决方案:

   exit = (Button) buildingSelect.findViewById(R.id.exit);
   colonyHut = (ImageView) buildingSelect.findViewById(R.id.colonyHut);

【讨论】:

  • 这是有道理的。谢谢。
  • @saboehnke 欢迎您。如果有帮助,请 +1 这个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 2015-01-08
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2013-04-15
  • 2015-02-12
相关资源
最近更新 更多