wswld
wswld

Reputation: 1238

Problems with an Intent in Android

final Button OptButton = (Button) findViewById(R.id.OptButton);
        OptButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent OptionsSc = new Intent(this, OptionsActivity.class);
                startActivity (OptionsSc);
            }
        });

Eclipse keeps underlining the new Intent(this, OptionsActivity.class); part and I don't get why. Previously this call to the OptionsActivity was assigned to the hardware Search button and everything worked fine.

Upvotes: 1

Views: 140

Answers (5)

enginar
enginar

Reputation: 307

You can use like that;

Context c= MyActivityName.this;
Intent OptionsSc = new Intent(c, OptionsActivity.class);

Upvotes: 0

Aerrow
Aerrow

Reputation: 12134

Try any one of like this,

 Intent OptionsSc = new Intent(YourActivity.this, OptionsActivity.class);
                startActivity (OptionsSc);

or

startActivity(new Intent(YourActivity.this, OptionsActivity.class));    

Upvotes: 0

code_finder
code_finder

Reputation: 1370

Write the following code in your button click listener...

  Intent intent = new Intent (Main.this,Second.class);
  StartActivity(intent);

Upvotes: 1

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

This is because you are inside an onClickListener class, and you this refers to object of current class, here either use getApplicationcontext or YourActivityName.this

This should solve your problem :)

Upvotes: 1

user
user

Reputation: 87064

In your case this doesn't refer to a valid Context(like when you use this in an Activity), instead it refers to the anonymous inner class OnCLickListener class(where is the onClick method declare). Instead you should use:

Intent OptionsSc = new Intent(YourActivityName.this, OptionsActivity.class);

Upvotes: 6

Related Questions