我是Java和xml的新手,当我启动我的应用程序大约5秒时,我希望有一个启动画面。我从堆栈溢出中获取了启动画面的代码来设置它但我不能让它运行因为某些原因可以有人帮助我! 干杯
我的飞溅课
package com.darraghoflaherty.competer.game;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 5000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
我的xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099FF">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/ss1"
android:id="@+id/ss1"
android:textColor="#ffffff"
android:textSize="260sp"/>
</RelativeLayout>
答案 0 :(得分:2)
首先改变代码的和平:
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
要
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(getApplicationContext(),Menu.class);
startActivity(mainIntent);
finish();
}
现在您的代码已清除,错误必须来自清单文件。 转到清单文件,将intent-filter的位置从Mainactivity更改为slashscreen活动。 这里是代码:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
答案 1 :(得分:1)
我的猜测是你没有更改清单中的启动器活动。 Android在AndroidManifest.xml中查找选择要先启动的活动。您的清单可能包含以下行:
<activity android:name=".Menu" 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=".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=".Menu"/>
将活动命名为XyzActivity
也是一个很好的约定,因此在您的情况下MenuActivity
和SplashActivity
。
答案 2 :(得分:0)
将此代码用于启动画面......
public class SPLASH extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000; // time to display the splash screen in MICROSECONDS
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while (_active && (waited < _splashTime)) {
sleep(100);
if (_active) {
waited += 100;
}
}
} catch (Exception e) {
} finally {
startActivity(new Intent(Splash.this,Menu.class));
finish();
}
};
};
splashTread.start();
}
}
并使用
使Splash活动成为启动器活动<activity
android:name=".SPLASH"
android:label="@string/app_name"
android:theme="@style/NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
希望这会对你有所帮助
答案 3 :(得分:0)
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
Thread timer = new Thread(){
public void run(){
try{
Thread.sleep(2000);
}
catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent mainIntent = new Intent(Splash.this,Menu.class);
startActivity(mainIntent );
finish();
}
}
};// end thread
timer.start();
答案 4 :(得分:-1)
嘿checkOut它我有点改进。感谢
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 5000;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
nav();
}
public void nav() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(Splash.this,Menu.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
@Override
protected void onPause() {
super.onPause();
finish();
}
}