1
package com.example.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Splash extends Activity {
    private Intent myintent;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        myintent = new Intent(this, MainActivity.class);
        splashScreen(1000); }

    public void splashScreen (final int x)
    {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(x);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startActivity(myintent);
                finish();
            }
        }).run();
    }

}

有代码,问题就在这里:SplashScreen 没有获取启动 XML 布局文件的内容视图......现在,我怀疑这是一个线程问题,并且不知何故线程在 setContentView 方法之前执行虽然该方法位于代码中 Thread 的 run 方法之前,所以我这样想是不合逻辑的,但我觉得这个启动画面不起作用的原因已经用完了

4

2 回答 2

6

更改thread.run()thread.start()http ://www.javafaq.nu/java-article1131.html

new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(x);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            startActivity(myintent);
            finish();
        }
    }).start();

实现 Splash 的更好方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    myintent = new Intent(this, MainActivity.class);

   new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            startActivity(myintent);
            finish();
        }
    }, 1000);
}
于 2013-02-07T21:12:28.400 回答
4
    package com.echo.myatlsnookpaid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

public class Splash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash);

        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(3500);
//                  sleep(100);
                }
                catch(InterruptedException e){
                    e.printStackTrace();
                } finally {
                    Intent openMain = new Intent(Splash.this, MainActivity.class);
                    startActivity(openMain);
                    finish();
                }
            }
        };
        timer.start();
    }

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

在你的清单中,给出

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

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

过滤到他的splashactivity。

于 2013-02-07T21:12:20.307 回答