[英]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...: 但是Eclipse不喜欢这样...:
View error message: http://i.stack.imgur.com/Zh8Id.png 查看错误消息: http : //i.stack.imgur.com/Zh8Id.png
But when I choose one of those methods, Eclipse wants the startActivity() again! 但是,当我选择其中一种方法时,Eclipse再次需要startActivity()!
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();
如果使用的是
Fragment
尝试getActivity();
instead of FirstActivity.this
, or if you in normal activity try getApplicationContext();
而不是
FirstActivity.this
,或者如果您在正常活动中尝试getApplicationContext();
FirstActivity.this
instead of FirstActivity.this
or just use this
. 而不是
FirstActivity.this
或仅使用this
。
You need to create an intent: 您需要创建一个意图:
startActivity(new Intent(CurrentActivity.this, NewActivity.class));
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 如果这是
Fragment
则使用
getActivity().startActivity(new Intent(getActivity(),YOURACTIVITY.class));
and if is this activity
then 如果这是
activity
startActivity(new Intent(currentActivity.this,YOURACTIVITY.class));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.