Custom button example not working
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;
开发者_如何学JAVA 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
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();
}
});
You are implementing the wrong OnClickListener. Try 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();
}
});
Replace the import android.content.DialogInterface.OnClickListener with
import android.view.View.OnClickListener
Rest of the code remain unchanged will execute the code perfectly.
精彩评论