Zephyer
Zephyer

Reputation: 343

Trying to call an activity, but having troubles with onClick

im trying to run this simple code (im a beginner :).

trying to run this. the // text is usually what i use for buttons. however, i saw this switch technique that i wanted to try, it seemed more efficient. however, i get errors related to the onClick (something about the ( ) and that 'void is an invalid type'). i have no idea what can cause this. just wanna access the buttons. can anyone please tell me why?

Thanks!

    package com.experiment.fewops;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class FewOptions extends Activity {
        /** Called when the activity is first created. */

        final Button sexy = (Button) findViewById(R.id.buttonSexy);
        final Button text = (Button) findViewById(R.id.buttonText);

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

    //        sexy.setOnClickListener(new View.OnClickListener() {
    //          
    //          @Override
    //          public void onClick(View v) {
    //              Intent intent = new Intent(this, SexyPage.class);
    //              startActivity(intent);
    //          }
    //      });

    @Override
    public void onClick(View v) {


        switch(v.getId()){
        case R.id.buttonSexy:
            Intent intent = new Intent(this,SexyPage.class);
            startActivity(intent);
            break;
        }

    };


}

}

Upvotes: 0

Views: 289

Answers (3)

Chris
Chris

Reputation: 23171

There are actually 2 problems here: First, as @Saiesh said, if you want to implement the click listener at the class level, you need to change your class declaration to implment OnClickListener. So your declaration will look like

public class FewOptions extends Activity implements OnClickListener{

The second problem (and the reason you're getting the error about void being a bad type) is that you're declaring the onClick method in the body of your onCreate method. move the declaration of the onClick method outside the closing brace (}) of the onCreate method and that error should go away.

One more note: after you make the 2 fixes above, don't forget to add your class as the click listener for the button:

 sexy.setOnClickListener(this);

Upvotes: 1

Saiesh
Saiesh

Reputation: 641

Well the solution is that to use this onClick() method your class needs to implement the onClickListener interface . Thus your class heading should be something like this

public class FewOptions extends Activity implements onClickListener
{
       //Eclipse will automatically ask you to override the onClick() method
}

So thats the solution :)

Upvotes: 0

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

You need to pass right context

Intent intent = new Intent(FewOptions.this,SexyPage.class);
FewOptions.this.startActivity(intent);

Upvotes: 0

Related Questions