2011-04-01 22 views
5

Soy un novato en el desarrollo de aplicaciones de Android. Estoy usando el ejemplo Form Stuff dado en http://developer.android.com/resources/tutorials/views/hello-formstuff.html. En esto he usado el ejemplo de botón personalizado.Ejemplo de botón personalizado que no funciona

Mi código es la siguiente:

package com.example.helloformstuff; 

    import android.app.Activity; 
    import android.content.DialogInterface.OnClickListener; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.Toast; 

    public class HelloFormStuff extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on clicks 
       Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show(); 
      } 
     });  

    } 
    } 

Su proyección siguiente error:

The type new DialogInterface.OnClickListener(){} must implement the inherited 
    abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int) 
    - The method setOnClickListener(View.OnClickListener) in the type View is not 
    applicable for the arguments (new DialogInterface.OnClickListener(){}) 

no soy capaz de averiguar la razón de tal error.

Por favor, ayúdenme en esto.

Gracias

Pankaj

Respuesta

1

Vuelva a colocar la importación android.content.DialogInterface.OnClickListener con

android.view.View.OnClickListener importación

resto del código se mantienen sin cambios ejecutará el código perfectamente .

+0

¿Sabes por qué esto está escrito de esta manera? ¿Por qué no puede haber un solo oyente? – user219882

1

Está implementando el mal OnClickListener. Pruebe View.OnClickListener:

button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on clicks 
      Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
+0

Gracias a zack por responder para usar su código, tengo que eliminar la importación android.content.DialogInterface.OnClickListener; porque ya importé la vista? –

3

Está utilizando el OnClickListener incorrecto. Utilice este código en su lugar:

button.setOnClickListener(new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // Perform action on clicks 
      Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
+0

Gracias Roflcoptr por responder. He modificado el código de acuerdo con su sugerencia, pero ahora recibo un error de seguimiento: - DialogInterface no se puede resolver con un tipo \t - El método setOnClickListener (View.OnClickListener) en el tipo View no es \t aplicable para los argumentos (nuevo OnClickListener() {}) –

+0

¿Ha importado el paquete android.content? También puede usar android.content.DialogInterface.OnClickListener – RoflcoptrException

+0

Sí, puede ver desde el código anterior que importé el android.content.DialogInterface.OnClickListener –

Cuestiones relacionadas