简体   繁体   English

Android启动画面启动

[英]Android Splash screen to activity

I am creating an android app and when I go to debug it on my samsung galaxy the Splash activity loads first,as it should, but after that the app crashes/stops right after doing the "Splash" activity. 我正在创建一个Android应用程序,当我在三星银河上对其进行调试时,Splash活动首先加载,应该如此,但是在执行“ Splash”活动之后,该应用程序立即崩溃/停止。 It doesn't go to the "MainActivity" activity after the thread sleeps for 5 seconds. 线程休眠5秒钟后,它不会进入“ MainActivity”活动。 Does anyone know what might be causing the problem? 有谁知道是什么原因引起的? Plus after I tried debugging the app and loaded it onto my phone the app isn't even showing up. 另外,在我尝试调试该应用并将其加载到手机上之后,该应用甚至没有显示出来。 I am using Eclipse by the way. 我正在使用Eclipse。 It shows the app in my application manager on my phone but it doesn't show the icon in my app screen. 它会在手机上的应用程序管理器中显示该应用程序,但不会在我的应用程序屏幕上显示该图标。

Here is my Splash.java: 这是我的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();
}


}

Here is my Manifest: 这是我的清单:

<?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>

And here is my main activity which should start after the splash screen: 这是我的主要活动,应在启动屏幕之后开始:

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);

        }
    });

}

}

Oh.. Where do I start.. Let's go through all of the issues: 哦..我从哪里开始..让我们讨论所有问题:

1) Fix your manifest. 1)修正清单。 Definitely not the right way to declare your activities. 绝对不是宣告活动的正确方法。 Here is what it should look like: 它应该是这样的:

<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) Now let's fix the way you start your activity: 2)现在,让我们修复开始活动的方式:

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

3) Don't call finish() in onPause() - you break native activity lifecycle flow. 3)不要在onPause()调用finish() onPause() -您会破坏本机活动生命周期流程。 Call finish() right after you start new activity: 开始新活动后立即调用finish()

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

4) Instead of creating separate thread, just a create a Handler and post Runnable there with 5 seconds delay: 4)无需创建单独的线程,只需创建一个Handler并在其中延迟5秒Runnable发布到那里:

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

Here is entire file put together: 这是整个文件放在一起:

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);    
}

If it doesn't help - we definitely need to look at logcat output... 如果没有帮助-我们肯定需要查看logcat输出...

A simple way to achieve this if one is ready to compromise on drawing performance is to define custom theme with splash image that one wants to use as window background and use this custom theme as application theme 如果准备在绘图性能上折衷的话,实现此目标的一种简单方法是使用要用作窗口背景的初始图像定义自定义主题,并将此自定义主题用作应用程序主题。

styles.xml 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 AndroidManifest.xml

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

This would use the @drawable/background_image as the window.background. 这将使用@ drawable / background_image作为window.background。 As a result if the activities has transparent background then @drawable/background_image will be visible as activities background. 因此,如果活动具有透明背景,则@ drawable / background_image将显示为活动背景。 One can avoid this by setting appropriate color or drawable in onCreate of every activity programatically as 通过以编程方式在每个活动的onCreate中设置适当的颜色或可绘制颜色,可以避免这种情况

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

}

Check this for more information 检查此以获取更多信息

All what you need for a splash screen 启动画面所需的一切

SplashActivity.java SplashActivity.java

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 drawables create this bg_splash.xml 在可绘制对象中创建此bg_splash.xml

<?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>

In styles.xml create a custom theme styles.xml中创建一个自定义主题

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

and finally in AndroidManifest.xml specify the theme to your activity 最后在AndroidManifest.xml中为您的活动指定主题

<activity
        android:name=".activities.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Cheers. 干杯。

  @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 http://www.codehubb.com/android_splash_screen

I have added splash screen by using following code: 我通过使用以下代码添加了启动屏幕:

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() {

    }
}
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);
}

 }

For more reference click here http://androiddhina.blogspot.in/2015/05/android-splash-screen-example.html 有关更多参考,请单击此处http://androiddhina.blogspot.in/2015/05/android-splash-screen-example.html

Flow the below steps to Create your own splash screen 按照以下步骤创建自己的启动屏幕

  1. Create Activity1 and XML file 创建Activity1和XML文件
  2. Design the XML file and put the welcome message 设计XML文件并放入欢迎消息
  3. Crete Activity2 and XML file. 克里特岛Activity2和XML文件。
  4. Start the Activity1 开始活动1
  5. Start a Thread in Activity1 and sleep for 5 seconds 在Activity1中启动一个线程并休眠5秒钟
  6. Start Activity2 from the Thread 从线程启动Activity2

There is nothing called readymade splash screen in Android . Android中没有所谓的现成的启动画面。 we can achieve that using above steps. 我们可以使用上述步骤来实现。

In a single line, start Activity1 wait for 5 sec then start Activity2. 在一行中,启动Activity1等待5秒钟,然后启动Activity2。

So user will feel that first screen is splash screen. 因此用户会觉得第一个屏幕是启动屏幕。

You can download the complete code from below link 您可以从下面的链接下载完整的代码

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

i have faced the same problem.... i think you code is perfect for the app 我也遇到过同样的问题。...我认为您的代码非常适合该应用

u just try with this in the Intent Creation in your splash activity class 您只需在启动活动类的Intent Creation中尝试此操作

Intent openMainActivity = new Intent("android.intent.action.MAIN");//MAIN is the that u want to start //next after the current Activity Intent openMainActivity = new Intent(“ android.intent.action.MAIN”); // MAIN是您要在当前Activity之后开始的//

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM