Harsha M V Harsha M V - 1 year ago 132
Android Question

Error due to invalid combination of Toast and OnClickListener

I'm trying to use

. My code triggers the following error:

The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)

This is my code:

Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText name = (EditText)findViewById(R.id.name);
String Lname = name.getText().toString();

Toast.makeText(this, Lname, Toast.LENGTH_SHORT).show();


Answer Source

As The Kenny said, this is refering to the View.OnClickListener instead of your Activity. Change this, to MyActivity.this.

For example,

public class MyActivity extends Activity {
// ... other code here
Toast.makeText(MyActivity.this, Lname, Toast.LENGTH_SHORT).show();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download