执行AsyncTask时,启动屏幕显示空白的白色屏幕



我有一个应用程序,可以对基金交易所进行评级,并在RecyclerView中显示它们
在我对主题进行简单更改之前,我的应用程序运行良好。我把它从@android:style/Theme.AppCompat.Light.NoActionBar"改了

@android:style/Theme.NoTItleBar.Fullscreen
,因为我想在启动时加载应用程序时隐藏状态栏。


清单

        <!-- Splash Screen -->
    <activity android:name=".Splash"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

飞溅类

public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.initial_activity);
    new InitialViewLoad().execute();
}
private class InitialViewLoad extends AsyncTask<Void, Void, Long> {
    @Override
    protected Long doInBackground(Void ... params) {
        long before = System.currentTimeMillis();
        List<Fund> funds = FundPortfolio.get(getApplicationContext()).getFunds();
        new PricesFetcher().fetchItems(funds);
        for (Fund fund : funds) {
            new FinanceFetcher().fetchItems(fund);
            FundPortfolio.get(getApplicationContext()).updateFund(fund);
        }
        long after = System.currentTimeMillis();
        long time = after - before;
        return time;
    }
    @Override
    protected void onPostExecute(Long time) {
        Intent i = new Intent(Splash.this, MainActivity.class);
        synchronized (this) {
            try {
                if (SPLASH_DISPLAY_LENGHT - time > 0) {
                    wait(SPLASH_DISPLAY_LENGHT - time);

         }
                } catch (InterruptedException ioe) {
                    Log.e("Blah,Blah,Blah", "Blah", ioe);
                } finally {
                    startActivity(i);
                    finish();
                }
            }
        }
    }
}

如果我不运行InitialViewLoad().execute();,启动屏幕将正常显示
如果我在主要活动中添加了一笔资金,Splash Screen的工作原理与更改前一样
如果RecyclerView为空(不包含任何资金),则会显示一个空白的白色屏幕,而不是飞溅屏幕

我一辈子都搞不清楚是什么原因造成的
如果解释不好,我很抱歉。如果你对解决这个问题所需的任何代码有任何疑问,我很乐意提供
谢谢


更新##

initial_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="#1b5e20"
              android:gravity="center"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/logo"
        android:layout_gravity="center"/>
</LinearLayout>

更新2

修复了问题。我用了一个Timertask,它起到了的作用

protected void onPostExecute(Long time) {
    final Intent i = new Intent(Splash.this, MainActivity.class);
    handler = new Handler();
    timer = new Timer();
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    startActivity(i);
                    finish();
                }
            });
        }
    };
    if (SPLASH_DISPLAY_LENGHT - time > 0)
        timer.schedule(timerTask, SPLASH_DISPLAY_LENGHT - time);
    else
        timer.schedule(timerTask, 5000);
}

此解决方案适用于我的

为了快速打开应用程序,我们需要禁用即时运行。。。这种情况发生在API 23(棉花糖)和以上

遵循以下步骤:

文件->设置->构建、执行、部署->即时运行禁用所有四个选项

最新更新