How do I show a splash screen in Android?

风流意气都作罢 提交于 2019-12-24 01:09:59

问题


I want to show a splash screen when my app loads up, this is my Java code:

ImageView splash = (ImageView) this.findViewById(R.id.splashscreen);

splash.postDelayed(new Runnable(){
    splash.setVisibility(View.GONE);
}, 3000);

But I am getting the error "cannot resolve symbol" on the postDelayed() call. Also I get the "unexpected token" for

}, 3000);

Finally, this is my layout:

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ImageView
    android:id="@+id/splashscreen"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:src="@drawable/splash"
    android:layout_gravity="center"/> 

Logcat:

9:41:01 PM Throwable
       Read access is allowed from event dispatch thread or inside read-action only (see com.intellij.openapi.application.Application.runReadAction())
       Details: Current thread: Thread[ApplicationImpl pooled thread 239,4,main] 359404630
       Our dispatch thread:Thread[AWT-EventQueue-0 0.4.2#AI-133.970939, eap:true,6,main] 1198871553
       SystemEventQueueThread: Thread[AWT-EventQueue-0 0.4.2#AI-133.970939, eap:true,6,main] 1198871553

9:41:13 PM Compilation completed successfully in 12 sec

The manifest file:

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.conversation.splash"
        android:configChanges="orientation|keyboardHidden|screenSize"
        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>
</application>


回答1:


You can do as follows:

private static final int SPLASH_TIME_OUT = 2000;
private static final Handler mHandler = new Handler();

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

    mHandler.postDelayed(new Runnable() {

        @Override
        public void run() {

                startActivity(new Intent(getApplicationContext(), YourActivity.class)); 
                finish();   
        }
    }, SPLASH_TIME_OUT);    
}

Here activity_splash.xml is your splash Activity layout and YourActivity is the Activity you're going to next.




回答2:


private ImageView splash;


splash = (ImageView)findViewById(R.id.splashscreen);
new Handler().postDelayed(new Runnable(){
    @Override
public void run() {
        splash.setVisibility(View.GONE);
    }
}, 3000);

Try this.



来源:https://stackoverflow.com/questions/22599508/how-do-i-show-a-splash-screen-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!