简体   繁体   中英

Starting activity in Android - Eclipse always shows an error

I've got this code in my app that after five seconds open another activity.

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    startActivity(R.layout.activity_game);
  }
},5000);

But Eclipse doesn't like this...:

View error message: http://i.stack.imgur.com/Zh8Id.png

But when I choose one of those methods, Eclipse wants the startActivity() again!

What can I do?

To start activity use this:

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);

If you are using Fragment try getActivity(); instead of FirstActivity.this , or if you in normal activity try getApplicationContext(); instead of FirstActivity.this or just use this .

You need to create an intent:

startActivity(new Intent(CurrentActivity.this, NewActivity.class));

Starting Another Activity

Do something like

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(this, ActivityGame.class);
            startActivity(intent);
        }
    }, 5000);

instead.

if is this Fragment then use

getActivity().startActivity(new Intent(getActivity(),YOURACTIVITY.class));

and if is this activity then

startActivity(new Intent(currentActivity.this,YOURACTIVITY.class));

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