Android Splash Screen doesn't display

前提是你 提交于 2020-03-23 08:20:32

问题


I have the following splash screen defined (splash.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
  <item android:drawable="@color/colorGold"/>
  <item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/Banner2"/>
  </item>
</layer-list>

This is the style.xml:

<resources>

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

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">false</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name ="Theme.AppCompat.Light.NoActionBar" parent="ThemeOverlay.AppCompat"/>

    <style name="SplashTheme" parent="AppTheme">
      <item name="android:windowBackground">@drawable/splash</item>
    </style>
</resources>

This is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="OML_Android.OML_Android" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="28" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
    <service android:name=".SignalRSrv" android:label="Messenger" android:enabled="true"></service>
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
  <activity android:name=".activities.MainActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  </application>
</manifest>

The splash screen does not display at all, nothing. I copied the splash screen xml from a post here on stackoverflow and I've been at this for a couple of days now and have made zero progress. What am I missing?

* Update *

Made progress thanks to @Dipankar Baghel now get the splash screen, but the app crashes with unhandled exception, my activity code is:

using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
using Android.Content;
using Acr.UserDialogs;

namespace MyAndroidApp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true, NoHistory = true)]
    public class MainActivity : Activity
    {

        protected override void OnCreate(Bundle savedInstanceState)
        {
            // reset theme prior to loading layout
            SetTheme(Resource.Style.AppTheme);
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);
            RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;


            Android.Widget.Button MyButton = FindViewById<Android.Widget.Button>(Resource.Id.button_ok);

            UserDialogs.Init(this);

            MyButton.Click += (sender, e) =>
             {
                 Context context = this.ApplicationContext;
                 Intent intent = new Intent(this, typeof(Logon));
                 intent.SetFlags(ActivityFlags.NewTask);
                 StartActivity(intent);
                 Finish();
             };
        }
    }
}

This is the layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:src="@drawable/Banner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1" />

    <Button
        android:text="Ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_ok" />


</LinearLayout>

There's not much to the layout, but the error I'm getting is:

`Unable to instantiate activity ComponentInfo{MyAndroid.MyAndroid/MyAndroid.MyAndroid.activities.activity_main}:``

* Update *

Updated manifest code.


回答1:


layer-list is not a valid container in Activity xml. please replace with any layout container like LinearLayout,RelativeLayout and it should work.

Secondly Activity tag should be inside Application tag in menifest file like below -

Example -

<application>
  <activity></activity>
</application>

Your code should be look like below -

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
  <activity android:name=".activities.MainActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
</application>
</manifest>



回答2:


I think you have a misunderstanding here.

The "android:windowBackground" you had in SplashTheme is actualy not Android Splash screen. It is just a window will be displayed immediately after you launch your Application. This window will be displayed until the main Activity of application started.

Normally, you can see this windowBackground for the first time you launch your application (Cold start). But if you resume or Hot start application after Cold start you may not see the windowBackground.

For more detail please refer below link

https://developer.android.com/topic/performance/vitals/launch-time



来源:https://stackoverflow.com/questions/60518338/android-splash-screen-doesnt-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!