setContentView出错 - android.view.InflateException

时间:2013-05-04 14:26:18

标签: android android-layout android-image

我有一个闪屏,显示图像2秒。图像是PNG图像。当我启动我的APP时,它会崩溃并出现以下错误消息。我的应用程序崩溃的确切行是setContentView(R.layout.splash);

关键错误行: 我可以看到在膨胀布局时引用java.lang.OutOfMemoryError时出现了一些错误

请告诉我在哪里需要更正我的代码。

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:background="@drawable/splash">  

</RelativeLayout> 

Java代码:

 public class Splash extends Activity{
@Override
protected void onCreate(Bundle GoOnSudha) {
    // TODO Auto-generated method stub
    super.onCreate(GoOnSudha);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.splash);

    Thread thread = new Thread(){
        public void run(){
            try{
                sleep(2000);
            } catch (InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent dragDropIntent = new Intent("android.intent.action.DRAGDROP");
                startActivity(dragDropIntent);
            }
        }
    };
    thread.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

logcat的:

05-04 19:34:05.651: E/AndroidRuntime(28305): FATAL EXCEPTION: main
05-04 19:34:05.651: E/AndroidRuntime(28305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.DragDrop/com.example.DragDrop.Splash}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.os.Looper.loop(Looper.java:137)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.app.ActivityThread.main(ActivityThread.java:4441)
05-04 19:34:05.651: E/AndroidRuntime(28305): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 19:34:05.651: E/AndroidRuntime(28305): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 19:34:05.651: E/AndroidRuntime(28305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
05-04 19:34:05.651: E/AndroidRuntime(28305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
05-04 19:34:05.651: E/AndroidRuntime(28305): at dalvik.system.NativeStart.main(Native Method)
05-04 19:34:05.651: E/AndroidRuntime(28305): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.LayoutInflater.createView(LayoutInflater.java:606)
05-04 19:34:05.651: E/AndroidRuntime(28305): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-04 19:34:05.651: E/AndroidRuntime(28305): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.app.Activity.setContentView(Activity.java:1835)
05-04 19:34:05.651: E/AndroidRuntime(28305): at com.example.DragDrop.Splash.onCreate(Splash.java:18)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.app.Activity.performCreate(Activity.java:4465)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
05-04 19:34:05.651: E/AndroidRuntime(28305): ... 11 more
05-04 19:34:05.651: E/AndroidRuntime(28305): Caused by: java.lang.reflect.InvocationTargetException
05-04 19:34:05.651: E/AndroidRuntime(28305): at java.lang.reflect.Constructor.constructNative(Native Method)
05-04 19:34:05.651: E/AndroidRuntime(28305): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.LayoutInflater.createView(LayoutInflater.java:586)
05-04 19:34:05.651: E/AndroidRuntime(28305): ... 23 more
05-04 19:34:05.651: E/AndroidRuntime(28305): Caused by: java.lang.OutOfMemoryError
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.graphics.Bitmap.nativeCreate(Native Method)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:530)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:505)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:357)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:777)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.content.res.Resources.loadDrawable(Resources.java:1940)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.View.<init>(View.java:2844)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.View.<init>(View.java:2781)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.view.ViewGroup.<init>(ViewGroup.java:412)
05-04 19:34:05.651: E/AndroidRuntime(28305): at android.widget.RelativeLayout.<init>(RelativeLayout.java:179)
05-04 19:34:05.651: E/AndroidRuntime(28305): ... 26 more

2 个答案:

答案 0 :(得分:0)

您的图片尺寸太大。您的app超出了为您的应用分配的堆大小。因此,您将获得OutofMemoryException。堆大小因设备而异。

缩小位图或使用占用更少内存的其他图像。

您需要有效地加载位图。查看在内存中加载缩小版本的主题。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

还可以在onResume()中将drawable加载到布局中,并在onPause()中解除布局中的drawable绑定。

同时查看以下链接中的视频

https://www.youtube.com/watch?v=_CruQY55HOk

答案 1 :(得分:0)

2.5 mb太大而无法显示几秒钟。如果它是位图文件,请尝试更改为更加压缩的格式,因为它们很大并占用大量内存,例如Android所说的图像here

  

Bitmaps会占用大量内存,尤其是对于像照片这样的丰富图像。例如,Galaxy Nexus上的相机拍摄的照片最高可达2592x1936像素(5百万像素)。如果使用的位图配置是ARGB_8888(默认情况下从Android 2.3开始),那么将此图像加载到内存中需要大约19MB的内存(2592 * 1936 * 4字节),立即耗尽某些设备上的每个应用程序限制。