Question

I'm reading an Android's tutorial and I have a class that implements abstract class OnClickListener. The problem is that in tutorial when it override method onClick,this has only one parameter but my eclipse show me an error because the onClick method need two parameters.

Below my wrong code by tutorial,how Can I fix it?

public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button button = (Button) findViewById(R.id.form_button);
    button.setOnClickListener((android.view.View.OnClickListener) this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.form_button:
        final EditText edit_name = (EditText) findViewById(R.id.edit_name);
        final EditText edit_lastname = (EditText) findViewById(R.id.edit_lastname);
        Bundle bundle = new Bundle();
        bundle.putString("name", edit_name.getText().toString());
        bundle.putString("lastname", edit_lastname.getText().toString());
        Intent form_intent = new Intent(getApplicationContext(), Form.class);
        form_intent.putExtras(bundle);
        startActivity(form_intent);
        break;
    }

}

}
Was it helpful?

Solution

You need to import

 import android.view.View.OnClickListener

and not

 import android.content.DialogInterface.OnClickListener

I guess you have the wrong import of DialogInterface.OnClickListener

onClick(DialogInterface dialog, int which) takes 2 param

But View.OnClickListener's onClick takes only 1 param ie View

http://developer.android.com/reference/android/view/View.OnClickListener.html

OTHER TIPS

You may mistakenly imported the onClickListener on DialogInterface. You have to import android.view.onClickListener, because Button is a sub-class of View.

For more details check this link.

or do as below

implements View.OnClickListener
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top