Java Android 启动画面到活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22279077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Splash screen to activity
提问by mihirb
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. It doesn't go to the "MainActivity" activity after the thread sleeps for 5 seconds. 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. It shows the app in my application manager on my phone but it doesn't show the icon in my app screen.
我正在创建一个 android 应用程序,当我在我的三星 Galaxy 上调试它时,首先加载 Splash 活动,但之后应用程序在执行“Splash”活动后立即崩溃/停止。线程休眠 5 秒后,它不会转到“MainActivity”活动。有谁知道可能导致问题的原因是什么?另外,在我尝试调试该应用程序并将其加载到我的手机上之后,该应用程序甚至没有出现。顺便说一下,我正在使用 Eclipse。它在我手机上的应用程序管理器中显示该应用程序,但它没有在我的应用程序屏幕中显示该图标。
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);
}
});
}
}
回答by Pavel Dudka
Oh.. Where do I start.. Let's go through all of the issues:
哦..我从哪里开始..让我们来看看所有的问题:
1) Fix your manifest. Definitely not the right way to declare your activities. Here is what it should look like:
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) 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. Call finish()
right after you start new activity:
3)不要叫finish()
中onPause()
-你打破天然活性生命周期流程。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)而不是创建单独的线程,只需创建一个处理程序并在Runnable
那里延迟 5 秒发布:
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 输出......
回答by Bharat
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之后的下一个
回答by Jeevan
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
样式文件
<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. As a result if the activities has transparent background then @drawable/background_image will be visible as activities background. One can avoid this by setting appropriate color or drawable in onCreate of every activity programatically as
这将使用@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);
}
回答by user3465917
@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
}
回答by Jaykishan Sewak
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() {
}
}
回答by Dhina k
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
回答by awsleiman
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
在 drawables 创建这个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.xmlcreate 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.xmlspecify 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.
干杯。
回答by Nirmal Dhara
Flow the below steps to Create your own splash screen
按照以下步骤创建您自己的启动画面
- Create Activity1 and XML file
- Design the XML file and put the welcome message
- Crete Activity2 and XML file.
- Start the Activity1
- Start a Thread in Activity1 and sleep for 5 seconds
- Start Activity2 from the Thread
- 创建 Activity1 和 XML 文件
- 设计 XML 文件并放置欢迎信息
- Crete Activity2 和 XML 文件。
- 开始活动1
- 在 Activity1 中启动一个线程并休眠 5 秒
- 从线程启动Activity2
There is nothing called readymade splash screen in Android . we can achieve that using above steps.
在 Android 中没有什么叫做现成的闪屏。我们可以使用上述步骤来实现。
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
您可以从下面的链接下载完整的代码