Splashscreen无法显示

时间:2016-06-18 04:55:53

标签: android splash-screen

我有一个Android应用程序,我希望在6秒内显示启动画面,但启动应用程序时不显示启动画面,而是显示以前我的启动页面的屏幕。

以下是splashscreen.java文件:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Handler;
public class splashscreen extends AppCompatActivity {
    private static int SPLASH_TIME_OUT = 6000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splashscreen);

        new Handler().postDelayed(new Runnable() {

            // Using handler with postDelayed called runnable run method

            @Override
            public void run() {
                Intent i = new Intent(splashscreen.this, LoginActivity.class);
                startActivity(i);

                // close this activity
                //finish();
            }
        }, 6*1000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_splashscreen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的manifest文件 -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.doccorduser" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- Splash screen -->
        <activity
            android:name=".splashscreen"
            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=".LoginActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".RegActivity"
            android:label="@string/title_activity_reg" >
        </activity>
        <activity
            android:name=".Services"
            android:label="@string/title_activity_services" >
        </activity>
        <activity
            android:name=".viewExistingApp"
            android:label="@string/title_activity_view_existing_app" >
        </activity>
        <activity
            android:name=".DocumentAdvisor"
            android:label="@string/title_activity_document_advisor" >
        </activity>
        <activity
            android:name=".ChangePassword"
            android:label="@string/title_activity_change_password" >
        </activity>
        <activity
            android:name=".TrackStatus"
            android:label="@string/title_activity_track_status" >
        </activity>
        <activity
            android:name=".ChildRegister"
            android:label="@string/title_activity_child_register" >
        </activity>
        <activity
            android:name=".FathersDetails"
            android:label="@string/title_activity_fathers_details" >
        </activity>
        <activity
            android:name=".MothersDetails"
            android:label="@string/title_activity_mothers_details" >
        </activity>
        <activity
            android:name=".AppNumber"
            android:label="@string/title_activity_app_number" >
        </activity>
        <activity
            android:name=".SignUp"
            android:label="@string/title_activity_sign_up" >
        </activity>
    </application>

</manifest>

这是splashscreen.xml文件 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="16dp"
    android:paddingTop="56dp"
    android:paddingBottom="56dp"
    android:background="@drawable/back1"
    tools:context="com.example.lenovo.doccorduser.splashscreen">

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/foetus"
        android:layout_alignTop="@+id/textView4"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/doccord"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="DocCord"
        android:textStyle="bold"
        android:id="@+id/textView4"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:focusable="false"
        android:textColor="@android:color/holo_orange_light" />

</RelativeLayout>

请帮忙......

2 个答案:

答案 0 :(得分:0)

首先清理你的项目,

Build -> Clean Project

再次运行。它会起作用。

答案 1 :(得分:0)

您好,试试这段代码:

<强> SplashScreen.java

public class splashscreen extends AppCompatActivity {

@Override
protected void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);
    Thread timerThread = new Thread(){
        public void run(){
            try{
                sleep(6000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent intent = new Intent(splashscreen.this,MainActivity.class);
                startActivity(intent);
            }
        }
    };
    timerThread.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

<强> SplashScreen.xml

<ImageView
    android:src="@drawable/mobilewallpapers"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<强> AndroidManifest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

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

            </category>
        </intent-filter>
    </activity>
</application>

希望它有效;)