启动画面后无法重定向

时间:2013-10-17 10:47:40

标签: java android multithreading

我设置了一个启动画面,我希望它在5秒后重定向到我的主要活动,但我的应用程序始终保持在启动屏幕上,从不重定向。

我在SplashActivity.Java

中有以下代码
public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.SplashActivity);
        Thread timer = new Thread() {
            public void run() {
                try {
                    // sleep(R.integer.SplashActivityTime);
                    sleep(5000);
                } catch (InterruptedException iEx) {
                    iEx.printStackTrace();
                } finally {
                    Intent mainActivity = new Intent(
                           "com.myApp.myApp.MainActivity");

                    startActivity(mainActivity);
                }
            }
        };
        timer.start();
    }
}

Manifest我有:

<activity
    android:name="com.myApp.myApp.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="com.myApp.myApp.MainActivity"
    android:label="@string/app_name" >

    <intent-filter>
        <action android:name="com.myApp.myApp.MainActivity" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

6 个答案:

答案 0 :(得分:0)

更改您的意图:

Intent mainActivity = new Intent(this, MainActivity.class);

此外,哪个类是该sleep方法的所有者?

答案 1 :(得分:0)

删除清单文件中主要活动活动的intent-filter

实施启动画面的最佳方式click here

并将意图行开始更改为

Intent mainActivity = new Intent(SplashActivity .this, MainActivity.class);

答案 2 :(得分:0)

使用以下代码

Thread logoTimer = new Thread()
    {
        @Override
        public void run()
        {
            try
            {
                sleep(3000);
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }//End of try block
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }//End of catch block
            finally
            {
                finish();
            }//End of finally block
        }//End of run method
    };//End of anonymous thread inner class
    logoTimer.start();

Manifest

<activity
    android:name="com.myApp.myApp.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="com.myApp.myApp.MainActivity"
        android:label="@string/app_name" >
    </activity>

答案 3 :(得分:0)

也许试试这个:

公共类splashscreen扩展了Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash); // Your Splash Layout

        Handler handler = new Handler();

     // run a thread
        handler.postDelayed(new Runnable() {

            public void run() {

                // make sure we close the splash screen so the user won't come back when it presses back key

                finish();
                // start the home screen

                Intent intent = new Intent(splashscreen.this, TabDemo2Activity.class);
                splashscreen.this.startActivity(intent);

            }

        }, 5000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called

}
}

答案 4 :(得分:0)

使用Handler,因为它对我有用。

Handler handler = new Handler();

// run a thread after 2 seconds to start the home screen

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
                if (!mIsBackButtonPressed) {
                    // start next activity
                    Intent intent = new Intent(SplashScreen.this, SEOshopMainActivity.class);
                    startActivity(intent);
               } 
            }
        }, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called

答案 5 :(得分:0)

使用此代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Thread timer = new Thread() {
        public void run() {
            try {
                // sleep(R.integer.SplashActivityTime);
                sleep(5000);
            } catch (InterruptedException iEx) {
                iEx.printStackTrace();
            } finally {
                Intent mainActivity = new Intent(MainActivity.this,
                        SecondActivity.class);

                startActivity(mainActivity);
                finish();
            }
        }
    };
    timer.start();
}

并在清单中

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.sample.MainActivity"
        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="com.example.sample.SecondActivity"
        android:label="@string/app_name" >

    </activity>
</application>