2013-08-24 6 views
9

के अंदर ऑनक्लिकलिस्टर बटन कैसे सेट करें, मैं एंड्रॉइड के लिए नया हूं और संवाद के अंदर बटन के ऑनक्लिकलिस्टर को कॉल करते समय मुझे शून्य सूचक अपवाद मिल रहा है। मुझे क्या करना चाहिए।कस्टम संवाद

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

        } 
       }); 
      } 
     }); 

मैं आपकी मदद की सराहना करता हूं। यह कैसे मेरी संवाद एक्सएमएल की तरह लग रहा है: -

<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 अपने कस्टम संवाद में की है कि आप उन में प्रारंभ उसी तरह।

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

Thx यह अब काम करना शुरू कर दिया है ... –

+0

@ अभिषेकपतिदार अगर यह 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

संबंधित मुद्दे