因为 Thread 我的意图没有开始
because of Thread my intent is not starting
代码有什么问题?我找不到任何错误,启动画面不会被处理,因此意图无法正常工作。我为此初始屏幕尝试了不同的代码,它可以正常工作。我只想知道为什么这段代码不起作用。
public class Splash extends Activity {
@Override
protected void onCreate(Bundle sScreen) {
super.onCreate(sScreen);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void Run(){
try{
sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}finally {
Intent intent = new Intent("com.projects.ziham.learning.STARTINGPOINT");
startActivity(intent);
}
}
};
timer.start();
}
}
并且我更改了意图参数,它也不起作用示例:
Intent intent = new Intent(Splash.this,StartingPoint.class);
请在延迟一段时间后尝试以下重定向
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, yourActivity.class);
startActivity(i);
finish();
}
}, 1000);
请试试这个
Thread t=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("run called");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
System.out.println("finally called");
}
}
});
t.start();
试试这个:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(Splash.this, StartingPoint.class));
}
}, 1000);
您可以使用 Handler
.
而不是 Timer
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(Splash.this, StartingPoint.class);
startActivity(intent);
}
}, 3000);
因此,根据您的代码,假设这是一个启动画面是一个相当合理的假设。
虽然现在有几个解决您的 intent 未启动的答案,this 文章演示了实现启动画面的正确方法。
要么您必须使用 Handler
中的 postDelayed
方法,要么您需要 运行 UIThread 中的 startActivity
:
runOnUIThread(new Runnable(){
Intent intent = new Intent("com.projects.ziham.learning.STARTINGPOINT");
startActivity(intent);
});
将代码中的 运行 替换为 运行 并使用 Handler。
Thread timer = new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(MainActivity.this,SecondPage.class);
startActivity(intent);
}
}
});
timer.start();
代码有什么问题?我找不到任何错误,启动画面不会被处理,因此意图无法正常工作。我为此初始屏幕尝试了不同的代码,它可以正常工作。我只想知道为什么这段代码不起作用。
public class Splash extends Activity {
@Override
protected void onCreate(Bundle sScreen) {
super.onCreate(sScreen);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void Run(){
try{
sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}finally {
Intent intent = new Intent("com.projects.ziham.learning.STARTINGPOINT");
startActivity(intent);
}
}
};
timer.start();
}
}
并且我更改了意图参数,它也不起作用示例:
Intent intent = new Intent(Splash.this,StartingPoint.class);
请在延迟一段时间后尝试以下重定向
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, yourActivity.class);
startActivity(i);
finish();
}
}, 1000);
请试试这个
Thread t=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("run called");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
System.out.println("finally called");
}
}
});
t.start();
试试这个:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(Splash.this, StartingPoint.class));
}
}, 1000);
您可以使用 Handler
.
Timer
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(Splash.this, StartingPoint.class);
startActivity(intent);
}
}, 3000);
因此,根据您的代码,假设这是一个启动画面是一个相当合理的假设。
虽然现在有几个解决您的 intent 未启动的答案,this 文章演示了实现启动画面的正确方法。
要么您必须使用 Handler
中的 postDelayed
方法,要么您需要 运行 UIThread 中的 startActivity
:
runOnUIThread(new Runnable(){
Intent intent = new Intent("com.projects.ziham.learning.STARTINGPOINT");
startActivity(intent);
});
将代码中的 运行 替换为 运行 并使用 Handler。
Thread timer = new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(MainActivity.this,SecondPage.class);
startActivity(intent);
}
}
});
timer.start();