简体   繁体   English

如何设置自定义对话框内的onclicklistener按钮

[英]How to set onclicklistener button which is inside a custom dialog

I am new to android and I am getting null pointer exception when calling onclicklistener of a button inside the dialog. 我是android的新手,在调用对话框内按钮的onclicklistener时,我得到空指针异常。 what should I do. 我该怎么办。

btn_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog d=new Dialog(MainActivity.this);
                d.setContentView(R.layout.activity_main);
                d.setTitle("Add content");
                d.show();
                btnsubmit = (Button) findViewById(R.id.btn_submit);
                btnsubmit.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        String name = etName.getText().toString();
                        String phoneNo = etPhone.getText().toString();

                        String query = "INSERT INTO PHONE_CONTACTS(name,phone) values ('"
                                + name + "','" + phoneNo + "')";
                        sqlHandler.executeQuery(query);
                        showList();
                        etName.setText("");
                        etPhone.setText("");

                    }
                });
            }
        });

I will really appreciate your help. 我将非常感谢你的帮助。 this is how my dialog xml looks like:- 这是我的对话框xml的样子: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <TableLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content" >



        <TableRow

                android:id="@+id/tableRow1"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content" >



            <TextView

                    android:id="@+id/textView1"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="@string/name" />



            <EditText

                    android:id="@+id/et_name"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:ems="10" >

            </EditText>

        </TableRow>



        <TableRow

                android:id="@+id/tableRow2"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content" >



            <TextView

                    android:id="@+id/textView1"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="@string/phone" />



            <EditText

                    android:id="@+id/et_phone"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:ems="10" >

            </EditText>

        </TableRow>

        <Button
                android:id="@+id/btn_submit"
                android:layout_width="80dp"
                android:layout_height="40dp"
                android:text="@string/submit"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"/>
    </TableLayout>

</RelativeLayout>

You need to use the dialog object to initialize views 您需要使用对话框对象来初始化视图

   btnsubmit = (Button) d.findViewById(R.id.btn_submit);

Also if you have edittext's in your custom dialog make you initialize them in the same way. 此外,如果您在自定义对话框中有edittext,则可以以相同的方式初始化它们。

   EditText etName = (EdiText) d.findViewById(R.id.et_Name);
   EditText etPhone = (EdiText) d.findViewById(R.id.et_Phoe);

You can dismiss dialog by calling dialog.dismiss() 您可以通过调用dialog.dismiss()来关闭对话框

       final Dialog d=new Dialog(MainActivity.this);
            d.show();
            btnsubmit = (Button) d.findViewById(R.id.btn_submit);
            btnsubmit.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    d.dismiss();
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM