安卓 - 初始屏幕显示空白



启动画面活动仅显示空白屏幕。代码如下:

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class SplashScreen extends AppCompatActivity {
    private static int SPLASH_TIME_OUT = 3000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        try {
            Log.v(" gonna  AsyncTask"," execute AsyncTask");
            new AsyncTask<String, String, String>() {
                @Override
                protected void onPreExecute() {
                    Log.v(" PreExecute AsyncTask"," execute AsyncTask");
                }
                @Override
                protected String doInBackground(String... arg0) {
                    Log.v(" in doInBackground"," execute AsyncTask");
                    try {
                        Log.v(" sleep doInBackground"," execute AsyncTask");
                        Thread.sleep(SPLASH_TIME_OUT);
                    } catch (Exception e) {
                        Log.v(" Exception "," execute AsyncTask");
                        e.printStackTrace();
                    }
                    return null;
                }
                @Override
                protected void onPostExecute(String unused) {
                    Log.v(" onPostExecute "," execute AsyncTask");
                    Intent i = new Intent(SplashScreen.this,
                            MainActivity.class);
                    Log.v(" starting  Activity "," execute AsyncTask");
                    startActivity(i);
                }
            }.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }// end of onCreate
}// end of class SplashScreen

XML 布局文件具有:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.istiaqueahmed.archeology.SplashScreen">
<ImageView
    android:id="@+id/splash_img"
    android:layout_width="170dp"
    android:layout_height="112dp"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="130dp"
    android:src="@drawable/splash"
    />
<TextView
    android:id="@+id/developed_by"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:text="@string/developed_by"
    android:layout_below="@id/splash_img"
    android:textStyle="bold"
    android:textSize="13sp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="30dp"
    />

</RelativeLayout>

安卓清单文件有 -

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        >
        <activity
            android:name=".SplashScreen"
            android:noHistory="true"
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         ........
         </application>

而风格.xml有——

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

也许活动需要一些时间才能执行setContentView,同时应用程序进入MainActivity

如何防止应用显示空白屏幕,而是在预期时间段内显示初始屏幕?

您的应用程序已冻结,因为您正在休眠主线程。

Thread.sleep(SPLASH_TIME_OUT); // This will sleep your main thread.

而不是AsyncTask延迟使用Handler

使用 CountDownTimer 而不是 AsyncTask

private static long SPLASH_TIME_OUT = 3000;
new CountDownTimer(SPLASH_TIME_OUT, 1000) {
        public void onTick(long millisUntilFinished)
        {
        }
        public void onFinish()
        {
            Intent i = new Intent(SplashScreen.this,
                        MainActivity.class);
             startActivity(i);
        }
    }.start();
There is no need to used AsyncTask here.
Please try this code which helps you.
public class WelcomeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    startHandler();
}
private void startHandler() {
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(WelcomeActivity.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    };
    Handler handler = new Handler();
    handler.postDelayed(runnable, 3000);
}

}

最新更新