外部分配对于此过程来说太大,错误膨胀类,位图大小超过VM预算

时间:2010-11-03 13:13:39

标签: android android-emulator android-manifest

发布我的问题here too.

初学者......我正在做学校地图应用程序。

首先,我有4个按钮(level3,level4,level5,level6)的背景。

我尝试单击所有按钮,通常在点击3之后,下一个按钮将崩溃。(每个级别按钮进入级别地图,单击房间图像按钮,我将进入gallery-imageswitcher)

我在每个级别的地图中至少有5 ImageSwitchers个,因此总共有超过20个ImageSwitcher个画廊。

我使用Drawable代替Bitmap完成了所有内容。我有大量的图像(因为它是学校地图,显示我学校的所有照片,以及所有这些背景和ImageButton

我做了一些研究,有问题的是使用位图的人。可以解决的一些方法是system.gc();bitmap.recycle(不太确定如何使用它)

在我的主要背景中 - starttour.java

 public class StartTour extends Activity {  
       private SoundManager mSoundManager;  
       /** Called when the activity is first created. */  
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.ttour);  

           mSoundManager = new SoundManager();  
           mSoundManager.initSounds(getBaseContext());  
           mSoundManager.addSound(1, R.raw.beep);  

           //play music  
           final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.jazz);  
             mPlayer.start();  

           final Button button = (Button) findViewById(R.id.back);  

           button.setOnClickListener(new Button.OnClickListener() {    

               // Implement the OnClickListener callback  
               public void onClick(View v) {  
                 // do something when the button is clicked           
                   startActivity(new Intent("android.com.Android"));   
                   mSoundManager.playSound(1);  
                   mPlayer.stop();  
               }  
           });  

               final Button button1 = (Button) findViewById(R.id.level3);  
               button1.setOnClickListener(new Button.OnClickListener() {  
                   public void onClick(View v) {  
                       // Perform action on click  
                        startActivity(new Intent("android.com.Lvl3"));  
                        mSoundManager.playSound(1);  
                        mPlayer.stop();  

                   }  
                });  

               final Button button2 = (Button) findViewById(R.id.level4);  
               button2.setOnClickListener(new Button.OnClickListener() {  
                   public void onClick(View v) {  
                       // Perform action on click  
                        startActivity(new Intent("android.com.Lvl4"));  
                        mSoundManager.playSound(1);  
                        mPlayer.stop();  

                   }  
                });              


               final Button button3 = (Button) findViewById(R.id.level5);  
               button3.setOnClickListener(new Button.OnClickListener() {  
                   public void onClick(View v) {  
                       // Perform action on click  
                        startActivity(new Intent("android.com.Lvl5"));  
                        mSoundManager.playSound(1);  
                        mPlayer.stop();  

                   }  
                });  

               final Button button4 = (Button) findViewById(R.id.level6);  
               button4.setOnClickListener(new Button.OnClickListener() {  
                   public void onClick(View v) {  
                       // Perform action on click  
                        startActivity(new Intent("android.com.Lvl6"));  
                        mSoundManager.playSound(1);  
                        mPlayer.stop();  

                   }  
                });  
       }  
   }





<ImageView android:id="@+id/ilevel4" android:layout_width="wrap_content"
                android:layout_height="fill_parent" android:src="@drawable/level4map"
                android:layout_gravity="center"/>

<Button
        android:id="@+id/m412button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="92.5px"
        android:layout_marginTop="95px"
        android:background="@drawable/m412_button" />    

<Button
        android:id="@+id/m411button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="92.5px"
        android:layout_marginTop="248.8px"
        android:background="@drawable/m411_button" />

<Button
        android:id="@+id/m410button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="92.5px"
        android:layout_marginTop="334.8px"
        android:background="@drawable/m410_button" />   


<Button
        android:id="@+id/m405button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6px"
        android:layout_marginTop="246.5px"
        android:background="@drawable/m405_button" />

<Button
        android:id="@+id/m406button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6px"
        android:layout_marginTop="335px"
        android:background="@drawable/m406_button" />  

<Button
        android:id="@+id/m419button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260px"
        android:layout_marginTop="59.5px"
        android:background="@drawable/m419_button" />      

<Button
        android:id="@+id/m420button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260px"
        android:layout_marginTop="168.5px"
        android:background="@drawable/m420_button" />   

<Button
        android:id="@+id/m421button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260px"
        android:layout_marginTop="240.5px"
        android:background="@drawable/m421_button" />

<Button
        android:id="@+id/m422button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260px"
        android:layout_marginTop="335px"
        android:background="@drawable/m422_button" />              
</RelativeLayout>

在我的等级4.xml

     <ImageView android:id="@+id/ilevel4" android:layout_width="wrap_content"  
                    android:layout_height="fill_parent" android:src="@drawable/level4map"  
                    android:layout_gravity="center"/>  

   <Button  
           android:id="@+id/m412button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="92.5px"  
           android:layout_marginTop="95px"  
           android:background="@drawable/m412_button" />      

   <Button  
           android:id="@+id/m411button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="92.5px"  
           android:layout_marginTop="248.8px"  
           android:background="@drawable/m411_button" />  

   <Button  
           android:id="@+id/m410button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="92.5px"  
           android:layout_marginTop="334.8px"  
           android:background="@drawable/m410_button" />     


   <Button  
           android:id="@+id/m405button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="6px"  
           android:layout_marginTop="246.5px"  
           android:background="@drawable/m405_button" />  

   <Button  
           android:id="@+id/m406button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="6px"  
           android:layout_marginTop="335px"  
           android:background="@drawable/m406_button" />    

   <Button  
           android:id="@+id/m419button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="260px"  
           android:layout_marginTop="59.5px"  
           android:background="@drawable/m419_button" />        

   <Button  
           android:id="@+id/m420button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="260px"  
           android:layout_marginTop="168.5px"  
           android:background="@drawable/m420_button" />     

   <Button  
           android:id="@+id/m421button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="260px"  
           android:layout_marginTop="240.5px"  
           android:background="@drawable/m421_button" />  

   <Button  
           android:id="@+id/m422button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="260px"  
           android:layout_marginTop="335px"  
           android:background="@drawable/m422_button" />                   
   </RelativeLayout>

在level4.java

  public class Level4 extends Activity {  
        /** Called when the activity is first created. */  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.tlevel4);  

            final Button button1 = (Button) findViewById(R.id.m405button);  
            button1.setOnClickListener(new OnClickListener() {  
               public void onClick(View v) {  
                       // Perform action on click  
                    startActivity(new Intent("android.com.M405"));  
               }  
           });  

           final Button button2 = (Button) findViewById(R.id.m406button);  
           button2.setOnClickListener(new OnClickListener() {  
               public void onClick(View v) {  
                       // Perform action on click  
                    startActivity(new Intent("android.com.M406"));  
               }  
           });  
           }  
         }

在logcat中

11-03 12:35:55.194: ERROR/dalvikvm-heap(708): 483406-byte external allocation too large for this process.
11-03 12:35:55.194: ERROR/(708): VM won't let us allocate 483406 bytes
11-03 12:35:55.204: DEBUG/AndroidRuntime(708): Shutting down VM
11-03 12:35:55.213: WARN/dalvikvm(708): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
11-03 12:35:55.213: ERROR/AndroidRuntime(708): Uncaught handler: thread main exiting due to uncaught exception
11-03 12:35:55.254: ERROR/AndroidRuntime(708): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.com/android.com.Level3}: android.view.InflateException: Binary XML file line #7: Error inflating class java.lang.reflect.Constructor
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.os.Looper.loop(Looper.java:123)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.main(ActivityThread.java:3948)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Method.invoke(Method.java:521)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at dalvik.system.NativeStart.main(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class java.lang.reflect.Constructor
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.createView(LayoutInflater.java:512)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.Activity.setContentView(Activity.java:1626)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.com.Level3.onCreate(Level3.java:19)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): ... 11 more
11-03 12:35:55.254: ERROR/AndroidRuntime(708): Caused by: java.lang.reflect.InvocationTargetException
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.widget.ImageView.<init>(ImageView.java:103)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Constructor.constructNative(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.createView(LayoutInflater.java:499)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): ... 22 more
11-03 12:35:55.254: ERROR/AndroidRuntime(708): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:363)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:212)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:663)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.content.res.Resources.loadDrawable(Resources.java:1637)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.widget.ImageView.<init>(ImageView.java:113)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): ... 26 more

如果我先点击任意一个按钮并进入水平图,我会在看到imageswitcher图片时崩溃。

在logcat中

11-03 09:17:34.308: INFO/ActivityManager(564): Starting activity: Intent { action=android.com.M615 comp={android.com/android.com.M615_gallery} }
11-03 09:17:34.638: ERROR/dalvikvm-heap(712): 212800-byte external allocation too large for this process.
11-03 09:17:34.638: ERROR/(712): VM won't let us allocate 212800 bytes
11-03 09:17:34.638: DEBUG/AndroidRuntime(712): Shutting down VM
11-03 09:17:34.648: WARN/dalvikvm(712): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
11-03 09:17:34.648: ERROR/AndroidRuntime(712): Uncaught handler: thread main exiting due to uncaught exception
11-03 09:17:34.677: ERROR/AndroidRuntime(712): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:363)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:212)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:663)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.content.res.Resources.loadDrawable(Resources.java:1637)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.content.res.Resources.getDrawable(Resources.java:535)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.ImageView.resolveUri(ImageView.java:482)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.ImageView.setImageResource(ImageView.java:268)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.com.M615_gallery$AddImgAdp.getView(M615_gallery.java:71)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.makeAndAddView(Gallery.java:754)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.fillToGalleryRight(Gallery.java:703)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.layout(Gallery.java:634)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.onLayout(Gallery.java:339)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.ViewRoot.performTraversals(ViewRoot.java:929)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.os.Looper.loop(Looper.java:123)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.app.ActivityThread.main(ActivityThread.java:3948)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at java.lang.reflect.Method.invoke(Method.java:521)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at dalvik.system.NativeStart.main(Native Method)
如果有人可以提供帮助,那真的很感激。谢谢

PS:我在应用程序中有大量图像。我每个级别至少有5 ImageSwitcher个画廊,所以在我的应用程序中有20多个图像处理器。

1 个答案:

答案 0 :(得分:2)

我必须在这里做一些猜测,因为你的命名和行号并不匹配。

你有这一行,例如:

startActivity(new Intent("android.com.Lvl4"));

但您的活动名为“android.com.Level4”。当你将它称为“level4.xml”时,你正在给布局“tlevel4”充气。此外,您粘贴的错误是Level3。也许它们很相似,但线号很重要,所以最好发布相同的错误&amp;类。

我并不想挑剔,这些细节在尝试破译你不熟悉的代码时会产生很大的不同。

无论如何,在这种情况下,我认为很明显发生了什么。

错误1&amp; 2 这两个错误都是内存不足异常。在这两者中,你都要求操作系统为你提供加载大量资源的内存,操作系统只是告诉你你已经受够了。如果你发现它一直在发生,几乎可以肯定你在某个地方泄漏了记忆。你提到广泛使用drawables。您希望对这些操作非常小心,因为当您向视图添加drawable时,它会保留对视图的引用...其中包含对Context的引用。我发布的代码中没有看到任何明显的东西,但我敢打赌你有某种Context泄漏。阅读this article,看看他们谈论的内容是否响起。

另外,我注意到你的包名是android.com(即android.com.Lvl3)。也许你刚刚编辑了这个用于发布,但它应该以com开头,即com.yourProjectName.Lvl3。

哦,请不要这样做:

startActivity(new Intent("android.com.Android"));

只需调用this.finish();