繁体   English   中英

为什么我的闪屏不起作用?

[英]Why wont my splash screen work?

我是Java和xml的新手,当我启动我的应用程序大约5秒时,我希望有一个启动画面。 我从堆栈溢出中获取了启动画面的代码来设置它但我不能让它运行因为某些原因可以有人帮助我! 干杯

我的飞溅课

   package com.darraghoflaherty.competer.game;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

public class Splash extends Activity {

/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 5000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splashscreen);

    /* New Handler to start the Menu-Activity
     * and close this Splash-Screen after some seconds.*/
    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            /* Create an Intent that will start the Menu-Activity. */
            Intent mainIntent = new Intent(Splash.this,Menu.class);
            Splash.this.startActivity(mainIntent);
            Splash.this.finish();
        }
    }, SPLASH_DISPLAY_LENGTH);
}

我的xml代码

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#0099FF">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/ss1"
    android:id="@+id/ss1"
    android:textColor="#ffffff"
    android:textSize="260sp"/>




 </RelativeLayout>

首先改变这种代码的和平:

public void run() {
    /* Create an Intent that will start the Menu-Activity. */
    Intent mainIntent = new Intent(Splash.this,Menu.class);
    Splash.this.startActivity(mainIntent);
    Splash.this.finish();
}

public void run() {
    /* Create an Intent that will start the Menu-Activity. */
    Intent mainIntent = new Intent(getApplicationContext(),Menu.class);
    startActivity(mainIntent);
    finish();
}

现在您的代码已清除,错误必须来自清单文件。 转到清单文件,将intent-filter的位置从Mainactivity更改为slashscreen活动。 这里的代码:

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

我的猜测是你没有更改清单中的启动器活动。 Android在AndroidManifest.xml中查找选择要先启动的活动。 您的清单可能包含以下行:

<activity android:name=".Menu" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

这应该改为:

<activity android:name=".Splash" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".Menu"/>

将活动XyzActivity也是一个很好的约定,所以在你的案例中是MenuActivitySplashActivity

将此代码用于启动画面...

public class SPLASH extends Activity {


protected boolean _active = true;
protected int _splashTime = 3000; // time to display the splash screen in MICROSECONDS 


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



    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while (_active && (waited < _splashTime)) {
                    sleep(100);
                    if (_active) {
                        waited += 100;
                    }
                }
            } catch (Exception e) {

            } finally {

                startActivity(new Intent(Splash.this,Menu.class));
                finish();
            }
        };
             };
    splashTread.start();

    }

}

并通过使用使Splash活动成为启动器活动

<activity
        android:name=".SPLASH"
        android:label="@string/app_name"
        android:theme="@style/NoActionBar" 
        android:screenOrientation="portrait">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

希望这会对你有所帮助

/* New Handler to start the Menu-Activity
 * and close this Splash-Screen after some seconds.*/

        Thread timer = new Thread(){
        public void run(){
            try{
                Thread.sleep(2000);
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            finally{
                 Intent mainIntent = new Intent(Splash.this,Menu.class);

                startActivity(mainIntent );

                finish();
            }
        }

    };// end thread
    timer.start();

嘿checkOut它我有点改善。 谢谢

public class Splash extends Activity {

private final int SPLASH_DISPLAY_LENGTH = 5000;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);

   nav();
}

public void nav() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {


         Intent mainIntent = new Intent(Splash.this,Menu.class);
        startActivity(mainIntent);
       finish();

        }
    }, SPLASH_DISPLAY_LENGTH);
  }

@Override
protected void onPause() {
    super.onPause();
    finish();
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM