【问题标题】:Splash screen show blank white screen when performing AsyncTask执行 AsyncTask 时闪屏显示空白屏幕
【发布时间】:2016-09-10 07:37:59
【问题描述】:

我有一个应用程序可以对基金交易所进行评级并在 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();,则启动画面会正常显示。
如果我在主要活动中添加了基金,则启动画面会像更改前一样工作。
如果 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);
}

【问题讨论】:

  • 你的启动画面仅仅是加载到apk文件中的静态图片的简单视图吗?
  • 是的。我在 initial_activity 布局中将静态图像设置为背景
  • 我可以看看你的 initial_activity 布局吗?
  • 是的,我会更新我的帖子。抱歉回复晚了!
  • @JoshFischer 我解决了这个问题。我将 onPostExecute 更改为执行 Timertask 而不是 synchronized(this)

标签: android asynchronous android-asynctask splash-screen


【解决方案1】:

这个解决方案对我有用

为了快速打开应用程序,我们需要禁用即时运行... API 23(Marshmallow)及更高版本会发生这种情况

请按照以下步骤操作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 2023-03-18
    • 2015-10-03
    • 2020-07-17
    相关资源
    最近更新 更多