【问题标题】:Splash Screen - how to?启动画面 - 如何?
【发布时间】:2020-02-02 10:02:59
【问题描述】:

我的启动画面有问题,因为一旦启动应用程序,我首先看到 MainActivity,然后在启动画面之后,错误是什么?

public class MainActivity extends AppCompatActivity {

int time = 3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(MainActivity.this, Splash_screen.class);
            startActivity(i);
            finish();
        }
    }, time);

MainActivity 的 XML 代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<ImageButton
    android:id="@+id/b10"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginTop="450dp"
    android:layout_marginEnd="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b9"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="450dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b8"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginTop="250dp"
    android:layout_marginEnd="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b7"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="250dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginTop="50dp"
    android:layout_marginEnd="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b1"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="50dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Splash_screen 的 XML 代码。

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Splash_screen">

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="427dp"
    android:layout_height="588dp"
    app:srcCompat="@drawable/beb"
    tools:layout_editor_absoluteX="-8dp"
    tools:layout_editor_absoluteY="16dp" />

这些是 MainActivity 文件和 Splash_screen 的 xml 代码。 感谢您的帮助,我是 android studio 和 Stack Overflow 的新手

【问题讨论】:

    标签: java android splash-screen


    【解决方案1】:

    您从MainActivity 调用启动画面,因此创建活动然后显示启动画面是正常的;你应该关注@moon 的回答,但这有点不对:

    您必须编辑您的AndroidManifest.xml 以首先调用Splash_creen(确保Splash_screenActivity):

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="your.pack.age"
        android:installLocation="preferExternal" >
    
        <!-- Your permissions -->
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/app_image" <!-- You'll have to modify these 3 lines -->
            android:label="@string/app_name"
            android:roundIcon="@drawable/app_round_image"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity
                android:name=".Splash_screen">
                <intent-filter>
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity
                android:name="com.pitibi.boulas.MainActivity"/>
    
        </application>
    </manifest>
    

    然后你从Splash_screenOnCreate调用MainActivity

    public class Splash_screen extends AppCompatActivity {
    
    int time = 3000;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_splash_layout);
    
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(Splash_screen.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, time);
    

    【讨论】:

    • 谢谢,但你必须从主目录中删除意图
    【解决方案2】:

    首先进入清单并像这样更改意图过滤器

    <activity android:name=".SPLASH">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <activity android:name=".MainActivity" >
    
        </activity>
    

    这里是你需要先启动启动程序来启动启动活动,而不是在 splash.java 中进行

    int time = 3000;
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(Splash_screen.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }, time);
    

    【讨论】:

    • xml文件不行,必须写在闪屏的xml文件或者主activity的xml文件中??
    • 只需显示您的 menifest.xml 代码,我将更改为必需并提及更改。
    • youtube.com/watch?v=jXtof6OUtcE 检查此链接希望它能让你明白。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    相关资源
    最近更新 更多