krmax44
krmax44

Reputation: 334

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: https://i.sstatic.net/Zh8Id.png

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

What can I do?

Upvotes: 0

Views: 81

Answers (4)

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.

Upvotes: 1

Mohamed
Mohamed

Reputation: 656

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.

Upvotes: 3

Rajesh
Rajesh

Reputation: 2618

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));

Upvotes: 1

user3956566
user3956566

Reputation:

You need to create an intent:

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

Starting Another Activity

Upvotes: 1

Related Questions