5秒钟后,启动画面不会消失

时间:2015-03-21 12:15:14

标签: android

我'我们刚刚开始学习Android。我'我遇到的问题是5秒钟后无法隐藏启动画面。启动应用程序时,它会显示启动画面,并根据代码在5秒后消失。

请帮助,以下是您需要的所有代码。

Project Explorer

enter image description here

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.windows8.myapplication" >
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <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=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.windows8.myapplication.MAINACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>
</manifest>

splash.java

package com.example.windows8.myapplication;

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

public class splash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                } finally {
                    Intent openStartingPoint = new Intent("com.example.windows8.myapplication.MAINACTIVITY");
                    startActivity(openStartingPoint);
                }
            }
        };
    }

}

3 个答案:

答案 0 :(得分:3)

您不能在start字段上调用timer方法 - 因此您只是不启动该主题。

答案 1 :(得分:3)

您可以直接简单地完成此操作:

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent openStartingPoint = new Intent("com.example.windows8.myapplication.MAINACTIVITY");
                startActivity(openStartingPoint);
            }
        } ,5000);

答案 2 :(得分:0)

package com.example.windows8.myapplication;

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

public class splash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                } catch (InterruptedException e){
                    e.printStackTrace();
                } finally {
                    Intent openStartingPoint = new Intent("com.example.windows8.myapplication.MAINACTIVITY");
                    startActivity(openStartingPoint);
                }
            }
        };

您已启动的线程在技术上是正确的,以避免因等待5秒而导致的ANR

通常,您必须调用thread.run()来运行线程中的代码块。这应该是帖子线程声明完成。

此外,您正在线程中启动新活动。这些活动通常在UI线程上执行,因此声明处理程序并使用处理程序启动新活动。

    }

}

以下是正确的代码片段

public class MyActivity extends Activity
{
    Handler hander = new Handler(){
        public void handleMessage(Message m){
            Intent intent = new Intent (MyActivity.this, Next.class);
            startActivity(intent);
        }
    };
    pubilc void onCreate(Bundle ic)
    {
       //your code setContentView() etc....
       Thread toRun = new Thread()
       {
              public void run()
              {
                   try
                   {
                      sleep(5000);
                   } 
                   catch (InterruptedException e)
                   {
                      e.printStackTrace();
                ,  }
                    hander.sendMessage(1); 
              }
       }
       toRun.start();
    }
}