2013-05-25 22 views
1

我用android studio 0.1创建了一个初始屏幕,但是当我在usb的调试模式下在我的手机(nexus s)上测试它时,图像不显示。为什么?为什么我的闪屏不显示图像?

这是在MainActivity

package com.example.splash; 

import android.content.Intent; 
import android.os.Bundle; 
import android.app.Activity; 
import android.os.Handler; 
import android.view.Menu; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 

    Handler x = new Handler(); 
    x.postDelayed(new SplashHandler(), 7000); 



} 
class SplashHandler implements Runnable{ 

    public void run(){ 


    startActivity(new Intent(getApplication(), Main.class)); 
     MainActivity.this.finish(); 


    } 


} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

这是主要

package com.example.splash; 


import android.app.Activity; 

public class Main extends Activity { 

} 

这是Splash.xml

< ?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" android:background="@drawable/splash"> 

</LinearLayout> 
+0

你确定你有你的图片复制到这个文件夹 – Rajeev

+0

什么是图像文件的名称? splash或splash.jpg? –

+0

图像在文件夹中,它的名字是飞溅 – Malloc

回答

0

试试这个代码

MainActivi TY

public class MainActivity extends Activity { 

String EmpID; 
int requestCode; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.splash);   
    Thread timer = new Thread() { 
     public void run() { 
      try { 
       sleep(2500); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } finally { 
       startActivity(new Intent(getApplication(), Main.class)); 
       MainActivity.this.finish(); 
      } 
     } 
    }; 
    timer.start(); 
    } 
} 

Splash.xml记住开机画面应为png

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@drawable/splash"> 

+1

我在SO上读过一篇关于线程内睡眠不太好的设计。 – Raghunandan

+0

可能不确定。但从来没有为我创造一个prblm,因为有尝试,赶上,最后.. – MDMalik

+0

图像仍然没有出现。 – Malloc

1

不要使用应用程序上下文。要知道何时使用活动内容和应用程序上下文请通过commonsware检查以下尤其是答案的链接

When to call activity context OR application context?

我在后测试你的代码。它适用于我的设备三星Galaxy S3。只有我做的改变是在RelativeLayout中使用imageview,并在onCreate()中设置相同的图像。 我也使用了一个活动上下文。除此之外,你的代码很好。使用处理器

闪屏

public class Splash extends Activity { 
private static final int SPLASH_TIME = 2 * 1000;// 3 seconds delay 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
    ImageView iv= (ImageView) findViewById(R.id.imageView1); 
    iv.setBackgroundResource(R.drawable.afor); 
    try { 
      new Handler().postDelayed(new Runnable() { 

     public void run() { 

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

      Splash.this.finish(); 
     }  
    }, SPLASH_TIME); 
     } 

    } catch(Exception e){} 
} 
@Override 
public void onBackPressed() { 
    this.finish(); 
    super.onBackPressed(); 
} 
} 

splash.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" android:background="#ffffaa"> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true" 
    /> 

</RelativeLayout>