简体   繁体   English

创建启动画面后,应用程序不想启动

[英]App doesn't want to launch after creating a splash screen

I am working on an Android app source code that I downloaded from Github.我正在处理从 Github 下载的 Android 应用程序源代码。 The code has only one activity which is the main activity.该代码只有一项活动,即主要活动。 So I decided to create another activity to display a splash screen every time the user runs the application but after creating the splash screen the app doesn't run anymore.所以我决定创建另一个活动来在用户每次运行应用程序时显示启动画面,但在创建启动画面后应用程序不再运行。

A snippet of the Splash Activity class : Splash Activity 类的片段:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_splash);
    Thread myThread = new Thread() {
        @Override
        public void run() {
            try {
                sleep(2000);
                Intent intent = new Intent(getApplicationContext(),
                        MainActivity.class);
                startActivity(intent);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    myThread.start();
    //.....
}

And this is the AndroidManifest.xml file:这是AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.csab.soundboard">

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_activity_splash"
        android:parentActivityName=".SplashActivity"
        android:theme="@style/FullscreenTheme">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.csab.soundboard.MainActivity" />
    </activity>
</application>

</manifest>

This is what I get when I run the app on a virtual device:这是我在虚拟设备上运行应用程序时得到的结果:

截屏

You need a handler for startActivity(intent);你需要一个startActivity(intent);的处理程序startActivity(intent);

    //........
    //Handles the thread result of the Backup being executed.
    final Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            Intent intent = new Intent(getApplicationContext(),
                    MainActivity.class);
            startActivity(intent);
            finish();

        }
    };

    Thread myThread = new Thread() {
    @Override
    public void run() {
        try {
            sleep(2000);
            handler.sendEmptyMessage(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
   };
   myThread.start();
   //......

Use Handler instead of Thread.使用 Handler 而不是 Thread。

new android.os.Handler().postDelayed(new Runnable(){
    public void run(){
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
        finish();
    }
}, 2000);

Just delete <intent filter><intent filter/> from your MainActivity class in manifest.只需从清单中的 MainActivity 类中删除<intent filter><intent filter/> That have to work那必须工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM