Android Splash屏幕活动

时间:2014-03-08 15:55:17

标签: java android

我正在创建一个Android应用程序,当我在我的三星Galaxy上调试它时,Splash活动首先加载,因为它应该,但在此之后应用程序在执行“Splash”活动后立即崩溃/停止。线程休眠5秒后,它不会进入“MainActivity”活动。有谁知道可能导致问题的原因是什么?此外,在我尝试调试应用程序并将其加载到手机后,应用程序甚至没有显示出来。我顺便使用Eclipse。它在我的手机上显示应用程序管理器中的应用程序,但它不会在我的应用程序屏幕中显示该图标。

这是我的Splash.java:

package com.example.mihirsandroidapp;

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 openMainActivity =  new Intent("com.example.mihirandroidsapp.MAINACTIVITY");
                startActivity(openMainActivity);
            }
        }
    };
    timer.start();
}

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


}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:debuggable="true"
    android:allowBackup="true"
    android:icon="@drawable/cartooncat"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
       <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.mihirsandroidapp.SPLASH" />
            <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.mihirsandroidapp.MAINACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

这是我的主要活动,应该在启动画面后开始:

package com.example.mihirsandroidapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

int counter;
Button add, sub;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter = 0;
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter += 1;
            display.setText("Total is " + counter);


        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter -= 1;
            display.setText("Total is " + counter);

        }
    });

}

}

8 个答案:

答案 0 :(得分:12)

哦..我从哪里开始..让我们解决所有问题:

1)修复你的清单。绝对不是宣布你的活动的正确方法。这是它应该是什么样子:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:debuggable="true"
    android:allowBackup="true"
    android:icon="@drawable/cartooncat"
    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" >
    </activity>
</application>

2)现在让我们来解决你开始活动的方式:

Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);

3)不要在finish()中调用onPause() - 您破坏了本机活动生命周期流程。开始新活动后立即致电finish()

Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();

4)不要创建单独的线程,只需创建一个Handler并在那里发布Runnable,延迟时间为5秒:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        //this will be called after 5 seconds delay
    }
}, 5000);    

这是整个文件放在一起:

public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);
            startActivity(openMainActivity);
            finish();

        }
    }, 5000);    
}

如果它没有帮助 - 我们肯定需要查看logcat输出...

答案 1 :(得分:1)

如果一个人准备好在绘图性能上做出妥协,那么实现这一目标的一个简单方法是使用启动图像定义自定义主题,将其用作窗口背景并将此自定义主题用作应用程序主题

styles.xml

 <resources>
        <style name="CustomTheme" parent="android:Theme">
            <item name="android:windowBackground">@drawable/background_image</item>
            <item name="android:windowNoTitle">true</item>
        </style>
    </resources>

的AndroidManifest.xml

   <application
        android:debuggable="true"        
        android:icon="@drawable/icon"
        android:theme="@style/CustomTheme"
        android:label="@string/app_name">
        ...
</application>

这将使用@ drawable / background_image作为window.background。因此,如果活动具有透明背景,那么@ drawable / background_image将作为活动背景显示。人们可以通过以编程方式在每个活动的onCreate中设置适当的颜色或可绘制来避免这种情况

public void onCreate(){
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutResID);
activity.getWindow().setBackgroundDrawableResource(R.color.window_bg);

}

Check this for more information

答案 2 :(得分:1)

All what you need for a splash screen

SplashActivity.java

    #pragma omp parallel
    {
        #pragma omp single
        {
            A();
            #pragma omp task B();
            #pragma omp task C();
            D();
            #pragma omp taskwait
            #pragma omp task E();
            F();
        }
    }

In drawables create this bg_splash.xml

public class SplashActivity extends AppCompatActivity {

private final int SPLASH_DISPLAY_DURATION = 1000;

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);


    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {

            Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
            SplashActivity.this.startActivity(mainIntent);
            SplashActivity.this.finish();
        }
    }, SPLASH_DISPLAY_DURATION);
}}

In styles.xml create a custom theme

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:drawable="@color/app_color"/>

<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/ic_in_app_logo_big"/>
</item></layer-list>

and finally in AndroidManifest.xml specify the theme to your activity

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/bg_splash</item>
</style>

Cheers.

答案 3 :(得分:0)

  @Override
   protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Thread th = new Thread(new Runnable() {            /*create a new thread */
                              @Override
                              public void run() { /*
                                                                  * The purpose of this thread is to
                                                                  * navigate from one class to another
                                                                  * after some time
                                                                  */

                                     try {
                                            Thread.sleep(5000);
                                     } catch (InterruptedException e) {
                                            /*
                                             * We are creating this new thread because we don’t
                                             * want our main thread to stop working for that
                                             * time as our android stop working and some time
                                             * application will crashes
                                             */
                                            e.printStackTrace();
                                     }
                                     finally {
                                            Intent i = new Intent(MainActivity.this,
                                                          Splash_Class.class);
                                            startActivity(i);
                                            finish();
                                     }
                              }
                       });
          th.start(); // start the thread
   }

http://www.codehubb.com/android_splash_screen

答案 4 :(得分:0)

我使用以下代码添加了启动画面:

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_layout);
        initConstatnts();// method for intilizing any constants

        new Thread(new Runnable() {

            @Override
            public void run() {

                if (!isFinishing()) // checking activity is finishing or not
                {
                    try {
                        Thread.sleep(3000);//delay

                        Intent i = new Intent(getBaseContext(),
                                HomeActivity.class);
                        startActivity(i);

                        finish();
                    } 
                    catch (InterruptedException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }

        }).start();

    }

    private void initConstatnts() {

    }
}

答案 5 :(得分:0)

public class SplashScreen extends Activity 
 {
  private static int SPLASH_TIME_OUT = 2000;

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

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

@Override
 public void run() {

 Intent i = newIntent(SplashScreen .this, FirstActivity.class);

      startActivity(i);

      overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_in);


     finish();
  }
  }, SPLASH_TIME_OUT);
}

 }

如需更多参考,请点击此处http://androiddhina.blogspot.in/2015/05/android-splash-screen-example.html

答案 6 :(得分:0)

完成以下步骤以创建您自己的初始屏幕

  1. 创建Activity1和XML文件
  2. 设计XML文件并添加欢迎信息
  3. Crete Activity2和XML文件。
  4. 启动Activity1
  5. 在Activity1中启动线程并休眠5秒
  6. 从线程
  7. 启动Activity2

    Android中没有任何名为readymade的启动画面。我们可以通过上述步骤实现这一目标。

    在一行中,启动Activity1等待5秒,然后启动Activity2。

    因此用户会觉得第一个屏幕是启动画面。

    您可以从以下链接下载完整的代码

    http://javaant.com/splash-screen-android/#.VwzHz5N96Hs

答案 7 :(得分:-3)

我遇到了同样的问题....我认为你的代码非常适合应用

你只需在你的启动活动类

中的Intent Creation中尝试这个

Intent openMainActivity = new Intent(“android.intent.action.MAIN”); // MAIN是你想要开始的                                                                     //在当前活动之后的下一个