我有两个班级Splash.java
和Activity2.java
!!我尝试过简单的启动代码:
public class Splash extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread thread= new Thread()
{
@Override
public void run()
{
super.run();
startActivity(new Intent(Splash.this,Trial.class));
finish();
}
};
thread.start();
}
}
在我的清单中,我也提供了参赛作品。我的代码运行没有错误。代码R.layout.splash
如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/d"
android:layout_gravity="center"/>
</LinearLayout>
谢谢!
答案 0 :(得分:2)
试试这种方式
setContentView(R.layout.splashscreen);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
startActivity(new Intent(SplashScreen.this, Trial.class));
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/screen1">
</LinearLayout>
答案 1 :(得分:2)
public class Splash extends Activity {
RelativeLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
container = (RelativeLayout) findViewById(R.id.container);
Thread thread = new Thread(){
@Override
public void run() {
try {
sleep(3000);//sleeps for 3 second
Intent intent = new Intent(Splash.this,Activity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} catch (InterruptedException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
super.run();
}
};
thread.start();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
setSize();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
setSize();
}
}
private void setSize(){
if(container!=null) {
if (container.getChildCount() > 0) {
container.getChildAt(0).setMinimumHeight(getWindowManager().getDefaultDisplay().getHeight() * 4 / 10);
}
}
}
}
** your splash screeen layout **
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/shaded_rectangle"
android:id="@+id/container">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splas"/>
</RelativeLayout>
**manifest**
<activity
android:name=".collegeconnect.activity.Splash"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
答案 2 :(得分:0)
public class MainActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 4000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed
(
new Runnable()
{
@Override
public void run ()
{
Intent i = new Intent(MainActivity.this, HomeActivity.class);
startActivity(i);
finish();
}
},SPLASH_TIME_OUT
);
}
}