2013-08-24 21 views
9

私はアンドロイドを初めて使い、ダイアログ内のボタンのonclicklistenerを呼び出すときにnullポインタ例外が発生します。私は何をすべきか。カスタムダイアログの中にあるonclicklistenerボタンを設定する方法

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

        } 
       }); 
      } 
     }); 

本当にありがとうございます。 が、これは私のダイアログXMLがどのように見えるかです: - あなたのカスタムダイアログでのEditTextのを持っている場合

<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> 
+0

私はボタンの初期化にNullPointerExceptionが推測確認するために、スタックトレースを投稿してください。 – Raghunandan

答えて

10

あなたが望む

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

を初期化するために、ダイアログオブジェクトを使用する必要があります。また、あなたがそれらを初期化します同じ方法。

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

Thxそれは今働くようになった... –

+0

@AbhishekPatidar plsが答えを正しいものとしてマークするのに役立ちます。 – Raghunandan

+0

もう一度、ダイアログから外出することをお勧めします。ボタンをクリックするとダイアログ内のキャンセルと言う。 –

1

あなたは(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(); 
       } 
      }); 
+0

おかげで、この作品 – vuhung3990