-1

我有一个闪屏,显示 2 秒的图像。该图像是PNG图像。当我启动我的应用程序时,有时它会崩溃并显示以下错误消息。我的应用程序崩溃的确切行是 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();
}

}

日志猫:

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
4

2 回答 2

0

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

缩小位图或使用占用更少内存的不同图像。

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

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

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

另请查看以下链接中的视频

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

于 2013-05-04T15:07:00.610 回答
0

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

位图占用大量内存,尤其是对于照片等丰富的图像。例如,Galaxy Nexus 上的相机可拍摄高达 2592x1936 像素(5 兆像素)的照片。如果使用的位图配置是 ARGB_8888(Android 2.3 以后的默认配置),则将此图像加载到内存中大约需要 19MB 内存(2592*1936*4 字节),立即耗尽某些设备上的每个应用程序的限制。

于 2013-05-04T15:07:29.703 回答