Android 应用程序尝试启动时崩溃
我有一个启动屏幕,我想在我的主应用程序屏幕前运行。但是,当计时器结束时,应用程序崩溃。你知道为什么会这样吗?提前谢谢 下面是参考代码Android 应用程序尝试启动时崩溃,android,android-intent,Android,Android Intent,我有一个启动屏幕,我想在我的主应用程序屏幕前运行。但是,当计时器结束时,应用程序崩溃。你知道为什么会这样吗?提前谢谢 下面是参考代码 public class Splash extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.lay
public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread timer = new Thread() {
// Whatever is enclosed in the {} of method run(), runs when we
// start the application
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openMainScreen = new Intent("com.package.Main_Screen");
startActivity(openMainScreen);
}
}
};
timer.start();
}
}
写下面的代码
Intent openMainScreen = new Intent(this, MainActivity.class);
startActivity(openMainScreen);
而不是
Intent openMainScreen = new Intent("com.package.Main_Screen");
startActivity(openMainScreen);
并将MainActivity声明到Androidmanifest.xml文件中
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
它会解决你的问题。你为什么不干脆用这种意图
Intent openMainScreen = new Intent(Splash.this,Main_Screen.class);
startActivity(openMainScreen);
还要确保你已经像这样在清单中添加了活动
<activity android:name=".Main_Screen">
</activity>
您正在从不同的
线程
调用startActivity
。您必须从UI
线程
运行它。通过
public class Splash extends Activity {
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent openMainScreen = new Intent(Splash.this,
Main_Screen.class);
startActivity(openMainScreen);
}
}, 2000);
}
}
你必须这样打电话
Intent openMainScreen = new Intent(ClassName.this, MainActivity.class);
startActivity(openMainScreen);
您必须在清单文件中注册它
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
发布你的logcat错误报告你知道,我试着这样称呼它,但我得到了一个错误。我必须按照您的建议更改清单,以便像这样调用Intent openMainScreen=newintent(Splash.this,Main_Screen.class);谢谢,没问题。。你是韦尔康:)谢谢你。事实证明,我也不得不改变我的舱单