应用程序尝试启动Intent时崩溃

时间:2012-12-06 11:54:26

标签: android android-intent

我有一个启动画面,我想在我的主应用程序屏幕之前运行。但是,当计时器结束时,应用程序崩溃。关于为什么会这样的想法?提前谢谢。

以下是引用的代码

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();
    }
}

4 个答案:

答案 0 :(得分:1)

为什么不简单地使用这种意图,

Intent openMainScreen = new Intent(Splash.this,Main_Screen.class);
startActivity(openMainScreen);

并确保您已在此清单中添加了活动,

<activity android:name=".Main_Screen">
 </activity>

答案 1 :(得分:0)

写下面的代码

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>

它将解决您的问题。

答案 2 :(得分:0)

您正在呼叫来自不同startActivity的{​​{1}}。您必须从Thread UI运行它。您想要实现的目标可以通过

thread

答案 3 :(得分:0)

你必须像这样打电话

Intent openMainScreen = new Intent(ClassName.this, MainActivity.class);
startActivity(openMainScreen);

您必须在Manifest文件中注册

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
相关问题