简体   繁体   中英

Trouble launching Andengine Activity from OnClickListener

I have an Activity which is just for displaying the main menu, and then I want to start an Andengine Activity after a Button click.

public class MainMenuActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menu_layout);

        TextView nadpis = (TextView) findViewById(R.id.nadpis);
        Typeface tf = Typeface.createFromAsset(getAssets(), "font/DiamondsPearls.ttf");
        nadpis.setTypeface(tf);

        Button playGame = (Button)findViewById(R.id.playgame);
        playGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(this, MonsterTap.class);
                startActivity(intent);
            }
        });
    }

}

I see an error referring to the line Intent intent = new Intent(this, MonsterTap.class); that reads

cannot resolve constructor intent(anonymous android.view.View.OnClickListener, java.lang.class)

What am I doing wrong?

Replace Intent intent = new Intent(this, MonsterTap.class); with Intent intent = new Intent(MainMenuActivity.this, MonsterTap.class); to properly pass the intent a Context .

As you have written it, the this keyword is referring to the anonymous inner class representing your OnClickListener .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM