我在Android应用程序上工作,我只是尝试在应用程序中以欢迎的方式在空活动中添加背景。在xml文件的概述中,将显示图像,但是当我在模拟器或手机上尝试应用程序时,她不会显示。他们没有错误,我不明白。有人能帮帮我吗?
public class MainActivity extends Activity {
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, ListeContact.class);
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
startActivity(i);
this.finish();
}
}
activity_main.xml:
<?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:background="@drawable/fond">
</LinearLayout>
P.S。我的名为fond.png的图片位于名为drawable-hdpi,drawable-ldpi,drawable-mdpi,drawable-xdpi和drawable-xxdpi的5个文件夹中。
答案 0 :(得分:1)
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
Intent intent= new Intent(MainActivity.this, ListeContact.class);
startActivity(intent);
finish();
}
}, 3000);
我使用了您的代码并验证了..
答案 1 :(得分:0)
将图像放在名为drawable
的文件夹中,可以解决您的问题
答案 2 :(得分:0)
不要在UI线程中调用Thread.sleep(在您的情况下为onCreate方法)。这可能是原因。使用AsyncTask或备用。
答案 3 :(得分:0)
这就是我现在所需要达到的目标:
import android.os.Bundle;
import android.view.MotionEvent;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
public class SplashScreen extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
splashTread = new Thread() {
@Override
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
startActivity(new Intent(SplashScreen.this,
Home.class));
finish();
}
}
};
splashTread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(splashTread)
{
splashTread.notifyAll();
}
}
return true;
}
}
这是我的活动splash_screen.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash"
tools:context=".SplashScreen" >
</RelativeLayout>
答案 4 :(得分:0)
我认为问题在于你调用Thread.Sleep的方式。 尝试使用此代码进行初始屏幕:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(getApplicationContext(),
ListeContact.class));
finish();
}
}, 3000);