2

我正在尝试使用此 java 和 xml 代码实现启动画面。我从我的主要活动中创建了一个单独的 java 类,并将 java 代码放入其中。我在布局文件中创建了一个 xml 布局,并将 xml 代码放入其中。但是,我的应用程序正常显示,没有启动画面。它从不显示,但应用程序没有任何错误。Eclipse 也没有显示任何错误。这可能是什么原因?先感谢您。这是代码。

爪哇:

package com.carouseldemo.main;

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 {

    private final int SPLASH_DISPLAY_LENGHT = 1000;

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

        /* 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_LENGHT);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
                android:layout_height="fill_parent" android:src="@drawable/cat"
                android:layout_gravity="center"/>
        <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="Hello World, splash"/>
</LinearLayout>

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.carouseldemo.main"
      android:versionCode="1"
      android:versionName="1.0"
      >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" android:screenOrientation="portrait"/>
    <application android:icon="@drawable/buttonone" android:label="@string/app_name">

        <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest> 
4

7 回答 7

1
private static final int STOPSPLASH = 0;
private static final long SPLASHTIME =3000;



public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.splash);
            context = getApplicationContext();
            if(savedInstanceState == null){
                Message msg = new Message();
                msg.what = STOPSPLASH;
                spHandler.sendMessageDelayed(msg, SPLASHTIME);
            }
        }   


    private Handler spHandler = new Handler() {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
            case STOPSPLASH:
                gotoHomeScreen();
                break;
            default:
                break;
            }
            super.handleMessage(msg);
        }
    };

    public void gotoHomeScreen(){

        Intent i = new Intent();
            i.setClass(Splash.this,Home.class);
            startActivity(i);
            finish();
            spHandler = null;

    }

尝试这个。

在你的清单中

<activity android:name="Splash" android:configChanges="keyboardHidden" android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
于 2013-02-06T05:32:49.933 回答
1

MainActivity 是清单文件中的启动器活动。所以,飞溅没有显示。

将启动器活动 MainActivity 更改为 Splash 并为 MainActivity 编写另一个。

<application android:icon="@drawable/buttonone" android:label="@string/app_name">

        <activity android:name=".Splash" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" />
    </application>
于 2013-02-06T05:40:22.210 回答
1

用这个替换 Handler 部分:

Thread splashThread = new Thread() {
    public void run() {
                try {
                Thread.sleep(SPLASH_DISPLAY_LENGHT);
                } catch (Exception e) {
                } finally {
                Intent i = new Intent(Splash.this,Menu.class);
                startActivity(i);
                finish();
                }
            }
        };
    splashThread.start();
于 2013-02-06T05:15:36.453 回答
1

尝试如下:

 public class Splash extends Activity 
  {
   private final int SPLASH_DISPLAY_LENGHT = 1000;  
       @Override
         public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
               setContentView(R.layout.splash);
               new Thread(){
        public void run()
        {
            try
            {
                sleep(SPLASH_DISPLAY_LENGHT);
               Intent mainIntent = new Intent(Splash.this,Menu.class);
                         Splash.this.startActivity(mainIntent);
                        Splash.this.finish();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        };          
    }.start();
}

编辑:

  <activity android:name=".Splash" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
于 2013-02-06T05:24:32.140 回答
0

另一种实现使用的方法CountDownTimer

public class MyCounter extends CountDownTimer {

        public MyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {

            startActivity(new Intent(SplashScreen.this,
                    TabSample.class));
            finish();

            MCObject.cancel();



        }

        @Override
        public void onTick(long millisUntilFinished) {

        }

    }

OnCreate

     MyCounter MCObject;
    MCObject = new MyCounter(3000, 100);
    MCObject.start();
于 2013-02-06T05:19:36.707 回答
0

更换您的处理程序

new Thread(new Runnable() {
    public void run() {
        try {
            Thread.sleep(SPLASH_TIME);
            startActivity(intent);
            finish();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();
于 2013-02-06T05:20:01.497 回答
0

使用此代码,它可能会对您有所帮助。

public class SplashScreenActivity extends Activity {

    protected int _splashTime = 2000;

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

        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(_splashTime);
                }
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                startActivity(new Intent(SplashScreenActivity.this,
                        MainActivity.class));
                SplashScreenActivity.this.finish();
            }
        }
    };
    splashTread.start();
}

希望它会帮助你。

于 2013-02-06T05:42:13.320 回答