求android高手看看,想从一个activity延迟数秒自动跳到下一个activity,总是实现不了
publicclassHomeActivityextendsActivity{publicvoidonCreate(BundlesavedInstanceState){s...
public class HomeActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home); }
Handler handler = new Handler();
Runnable updateThread = new Runnable(){
public void run(){
Intent intent = new Intent();
intent.setClass(HomeActivity.this,LoginActivity.class);
HomeActivity.this.startActivity(intent);
handler.postDelayed(updateThread, 300);}
};
} 展开
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home); }
Handler handler = new Handler();
Runnable updateThread = new Runnable(){
public void run(){
Intent intent = new Intent();
intent.setClass(HomeActivity.this,LoginActivity.class);
HomeActivity.this.startActivity(intent);
handler.postDelayed(updateThread, 300);}
};
} 展开
3个回答
展开全部
页面跳转在handler发送消息之前。先执行跳转再延迟发送消息。handler.postDelayed(updateThread, 300);放置run方法外面,你这样写run方法都没有调用。
追问
我把handler.postDelayed(updateThread, 300);这句放在run方法的外面报错,放在Runnable updateThread = new Runnable(){ ...};这一句的外面还是有错。我真的不明白呀
追答
Handler handler = new Handler();
Runnable updateThread = new Runnable(){
public void run(){
Intent intent = new Intent();
intent.setClass(HomeActivity.this,LoginActivity.class);
HomeActivity.this.startActivity(intent);
finish();
}
};
handler.postDelayed(updateThread, 3000);这样应该没问题了呀。
展开全部
public class HomeActivity extends Activity implements Runnable {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(this).start();
}
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(this).start();
}
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-04-10
展开全部
把postDelayed这句放到updateThread外面才对吧……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询