执行AsyncTask时,启动画面显示空白屏幕

时间:2016-05-14 00:50:41

标签: android asynchronous android-asynctask

我有一个应用程序可以对基金交易进行评级并在RecyclerView中显示它们 在我对主题进行简单更改之前,我的应用程序工作正常。 我从@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();
                }
            }
        }
    }
}

Splash class

InitialViewLoad().execute();

如果我没有按正常运行 <?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> 启动画面显示 如果我在我的主要活动中添加了一个基金,那么启动画面就像在更改之前一样工作 如果RecyclerView为空(不包含任何资金),则显示空白屏幕而不是启动画面。

我不能为我的生活找出造成这种情况的原因。 对不起,如果说明不好。如果您对要解决的任何代码有任何疑问,我很乐意提供 谢谢!

更新##

initial_activity.xml

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

更新2

修正了问题。我使用了Timertask并且有效

explode

1 个答案:

答案 0 :(得分:1)

此解决方案适合我

为了快速打开应用程序,我们需要禁用即时运行... 这适用于API 23(Marshmallow)及以上

请按照以下步骤操作:

file - &gt;设置 - &gt;构建,执行,部署 - &gt;即时运行 禁用所有四个选项

相关问题