2010-06-08 10 views
6

मेरे आवेदन में, मेरे पास स्क्रीन पर प्रारंभ में एक बटन है, और बटन के onclick में, एक पॉपअप विंडो खुलनी चाहिए। पॉपअप विंडो में, मेरे पास एक बटनबटन है, और इस बटन के onclick है, मैं एक गतिविधि शुरू करना चाहता हूं। पॉपअप विंडो खुलती है, लेकिन मुझे समझ में नहीं आता कि पॉपअप विंडो के अंदर छविबटन के onclick को कैसे संभालना है।एंड्रॉइड में पॉपअप विंडो के अंदर बटन की ऑनक्लिक घटना को कैसे संभालना है

main.xml में, मेरे पास एक बटन है, और popup_example.xml में, मेरे पास एक छवि बटन है।

final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
final Button b=(Button)findViewById(R.id.btn); 
b.setOnClickListener(new OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main))); 
     pw.showAtLocation(v, Gravity.LEFT,0,0); 
     pw.update(8,-70,150,270); 

     //if onclick written here, it gives null pointer exception. 
     ImageButton img=(ImageButton)findViewById(R.id.home); 
     img.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Intent..... 
      } 
     }); 

     //if onclick is written here it gives runtime exception. 
    }); 

और मैं दो एक्सएमएल लेआउट .........

  1. main.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 
    
        <ImageButton 
         android:id="@+id/btn" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src="@drawable/ghj" /> 
    </LinearLayout> 
    
  2. है:

    मेरे जावा कोड इस प्रकार है

    popup_example.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:padding="10dip" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="#8E2323"> 
    
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
         android:orientation="vertical" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:padding="5px"> 
    
         <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          android:orientation="vertical" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:padding="5px" 
          android:background="#000000"> 
    
          <ImageButton android:id="@+id/home" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:focusable="true" 
           android:src="@drawable/vitalss" 
           android:layout_weight="1" 
           android:background="#8E2323"/>     
         </TableLayout> 
        </TableLayout> 
    </LinearLayout> 
    

उत्तर

16

आप पॉपअप दृश्य में बटन खोजने के लिए: ..

View pview = inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main)); 
PopupWindow pw = new PopupWindow(pview); 
      pw.showAtLocation(v, Gravity.LEFT,0,0); 
      pw.update(8,-70,150,270); 

       //if onclick written here, it gives null pointer exception. 
      ImageButton img=(ImageButton)pview.findViewById(R.id.home); 
      img.setOnClickListener(new OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        Intent..... 
       } 
     }); 
+0

अरे बहुत बहुत धन्यवाद !! यह काम। – henna

0

सबसे अच्छा समाधान :) पास एक्सएमएल

<ImageButton 
      android:id="@+id/save_btn" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      **android:onClick="onClick"** 
      android:contentDescription="@string/save" 
      android:src="@drawable/save" /> 

यह मुझ से wroked से onCclickMethod

1

यह इन्फ्लेटर में त्रुटि नहीं देगा और ठीक से काम करेगा:

LayoutInflater layoutInflater = getLayoutInflater(); 


     View pview = layoutInflater.inflate(R.layout.popup_example, (ViewGroup)findViewById(R.layout.main)); 
     PopupWindow pw = new PopupWindow(pview); 
     pw.showAtLocation(v, Gravity.LEFT,0,0); 
     pw.update(8,-70,150,270); 

      //if onclick written here, it gives null pointer exception. 
     ImageButton img=(ImageButton)pview.findViewById(R.id.home); 
     img.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Intent..... 
      } 
    }); 
संबंधित मुद्दे