我想在android中几秒后转移到另一个活动,我查找了StackOverflow并在this post中找到了代码。问题是它给了我一个错误的" CurrentActivity.this"它说" CurrentActivity不是一个封闭的类"
以下是代码:
package mirdak.smartfood;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
//delay in ms
int DELAY = 1000;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
}
}, DELAY);
}
}
我该如何解决这个问题?
答案 0 :(得分:1)
你应该传入Intent你的实际上下文,在你的情况下它将是
Intent intent = new Intent(MyActivity.this, NextActivity.class);