Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom button example not working

Tags:

android

I am newbie in android application development. I am using the Form Stuff example given on http://developer.android.com/resources/tutorials/views/hello-formstuff.html. In this i have used the custom button example.

My code is as below:

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

    }
   }

Its showing following 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(){})

I am unable to figure out the reason for such error.

Please help me on this.

Thanks

Pankaj

like image 978
Pankaj Khurana Avatar asked Apr 01 '11 10:04

Pankaj Khurana


1 Answers

You're using the wrong OnClickListener. Use this code instead:

   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();
        }
    });   
like image 98
RoflcoptrException Avatar answered Sep 30 '22 12:09

RoflcoptrException