Question

i keep getting this error on btn.setOnClickListener(this). I have the same code as before however i never got this error until i opened up my eclipse a few minutes ago. Help soon would be great thank you.

package com.example.bmisummative;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class weightloss extends Activity {
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weightloss);
        btn = (Button) findViewById(R.id.homeButton);
        btn.setOnClickListener(this);
    }

        public void onClick(View v) {
        if (v.getId() == R.id.homeButton) {
            startActivity(new Intent(this, MainActivity.class));
        }

    }
}

No correct solution

OTHER TIPS

You are not implementing OnClickListener. You need to do this in oder to set your onClickListener like this:

 btn.setOnClickListener(this);

You need to do this:

public class weightloss extends Activity implements OnClickListener {...}

You should implement OnClickListener

public class weightloss extends Activity
implements OnClickListener
{
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.weightloss);
      btn = (Button) findViewById(R.id.homeButton);
      btn.setOnClickListener(this);
   }

   public void onClick (View v)
   {
      // Button tap detected! Do stuffs!
   }

}

You are missing implements OnClickListener

public class weightloss extends Activity implements OnClickListener {}

There is another clean way to do it without adding implements OnClickListener:

btn = (Button) findViewById(R.id.homeButton);
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
           startActivity(new Intent(this, MainActivity.class));

     }
});

I prefer the latter as it is easier to read when dealing with lots of buttons.

just write

public class weightloss extends Activity implements OnclickListner 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top