2010-06-08 9 views
6

En mi aplicación, inicialmente tengo un botón en la pantalla, y en onclick del botón, se debe abrir una ventana emergente. En la ventana emergente, tengo un botón de imagen, y onclick de este botón, quiero comenzar una actividad. Se abre la ventana emergente, pero no entiendo cómo manejar el onclick del botón imagen dentro de la ventana emergente.cómo manejar el evento onclick del botón dentro de la ventana emergente en android

En main.xml, tengo un botón, y en popup_example.xml, tengo un botón de imagen.

código

Mi Java es la siguiente:

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. 
    }); 

y tengo dos diseños xml .........

  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> 
    

Respuesta

16

Tienes que encontrar el botón en la vista emergente:

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

hey muchas gracias! Esto funciona. – henna

0

mejor solución :) Pass onCclickMethod desde XML

<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" /> 

que wroked de mí ..

1

Esto no dará error en Inflater y funciona correctamente:

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..... 
      } 
    }); 
Cuestiones relacionadas