0

我有两个活动:主要和飞溅。我的目标是显示 Splash 活动 5 秒,然后开始 Main 活动。当我在计算机的模拟器上启动 Splash 活动时,一切正常。当正在运行的设备是我的手机 Nexus 5(插入计算机)时,手机会显示一点点(2 - 3 秒)的 Splash 活动,然后进入主屏幕,然后(2 - 3 秒后)开始主要活动。当我使用 apk 文件在手机上安装应用程序时,一切正常。我的主要课程没有什么特别的。如果这很重要,我正在使用 Android Studio。我的启动画面只是普通的图像。我的飞溅活动:

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
            Thread timer = new Thread() {
                public void run() {
                    try {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        startActivity(new Intent("com.example.app.MAIN"));
                    }
                }
            };
            timer.start();
        }

        @Override
        protected void onPause() {
            super.onPause();
            finish();
        }

日志猫:

04-27 16:59:34.655    2502-2502/? I/Crashlytics﹕ Initializing Crashlytics 1.1.10.12
04-27 16:59:34.735    2502-2506/? D/dalvikvm﹕ GC_CONCURRENT freed 316K, 3% free 16924K/17272K, paused 12ms+5ms, total 34ms
04-27 16:59:34.735    2502-2523/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 12ms
04-27 16:59:34.735    2502-2516/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 12ms
04-27 16:59:34.745    2502-2502/? E/OpenUDID﹕ Initialisation isn't done
04-27 16:59:34.775    2502-2506/? D/dalvikvm﹕ GC_CONCURRENT freed 444K, 3% free 16992K/17468K, paused 2ms+2ms, total 20ms
04-27 16:59:34.775    2502-2515/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 4ms
04-27 16:59:34.785    2502-2502/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 5ms
04-27 16:59:34.785    2502-2530/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 5ms
04-27 16:59:34.785      761-809/? I/ActivityManager﹕ Killing 2332:com.android.providers.calendar/u0a1 (adj 15): empty #17
04-27 16:59:34.835    2502-2502/? V/AmazonInsightsSDK﹕ Firing Session Event: _session.start
04-27 16:59:34.835    2502-2502/? I/AmazonInsightsSDK﹕ Amazon Insights SDK(2.1.16.0) initialization successfully completed
04-27 16:59:34.845    2502-2536/? I/AmazonInsightsSDK﹕ Attempting to retrieve variation(s) for project(s):'Sponsored in feed'
04-27 16:59:34.865    2502-2506/? D/dalvikvm﹕ GC_CONCURRENT freed 462K, 3% free 17024K/17516K, paused 3ms+3ms, total 30ms
4

1 回答 1

0

您需要从 UI 线程调用 startActivity。一种简单的方法是使用 AsyncTask 而不是直接创建自己的线程。

    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            startActivity(new Intent("com.example.app.MAIN"));
        }

    }.execute();
于 2014-09-04T22:25:12.050 回答