有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Dialg ImageView NullPointerException setOnClickListener

我有一个NullpointerException,但我不知道为什么!在我苦苦挣扎的时候,请你帮助我:

这是我的XML资源:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:background="#FFFFFF" >

<LinearLayout
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    安卓:layout_margin="20dp"
    安卓:orientation="horizontal" >

    <LinearLayout
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_margin="3dp"
        安卓:gravity="right"
        安卓:orientation="vertical" >

        <TextView
            安卓:id="@+id/txt_speciality_type"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="Name: " />

        <TextView
            安卓:id="@+id/textView4"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="add:" />

        <TextView
            安卓:id="@+id/textView5"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="phone :" />
    </LinearLayout>

    <LinearLayout
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_margin="3dp"
        安卓:orientation="vertical" >

        <TextView
            安卓:id="@+id/textView3"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="abc abc abc" />

        <TextView
            安卓:id="@+id/textView44"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="abc" />

        <TextView
            安卓:id="@+id/textView55"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:text="230020202" />
    </LinearLayout>
</LinearLayout>

<ImageView
    安卓:id="@+id/imageViewClose"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_alignParentRight="true"
    安卓:src="@drawable/close_selector" />
 </RelativeLayout>

在我的Java指令下面: setContentView在实际TextView使用之前

final Dialog dialog = new Dialog(searchActivity.this);
dialog.setContentView(R.layout.custom_dialog);``
ImageView closeBtn =(ImageView)findViewById(R.id.imageViewClose);
closeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    finish();
                }
            });

Logcat erros:

java.lang.NullPointerException
        at com.itraan.Test.searchActivity$2.onItemClick(searchActivity.java:902)
        at 安卓.widget.AdapterView.performItemClick(AdapterView.java:308)
        at 安卓.widget.AbsListView.performItemClick(AbsListView.java:1483)
        at 安卓.widget.AbsListView$PerformClick.run(AbsListView.java:3485)
        at 安卓.widget.AbsListView$3.run(AbsListView.java:4843)
        at 安卓.os.Handler.handleCallback(Handler.java:733)
        at 安卓.os.Handler.dispatchMessage(Handler.java:95)
        at 安卓.os.Looper.loop(Looper.java:157)
        at 安卓.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

提前谢谢你


共 (0) 个答案