出内存:加入10KB ImageButtons后堆大小错误 [英] Out of memory: Heap Size Error after adding 10KB ImageButtons

查看:275
本文介绍了出内存:加入10KB ImageButtons后堆大小错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eclipse中工作的一个Android应用程序,其与闪屏(这里怀疑内存泄漏一些研究完成之后)开始。闪屏进入由仅有7功能的按钮,一些Java PApplet主菜单。直到我换了7假人(ImageButton的)PNG图像到敲定7 png图片的整个应用程序是完美的工作。这些PNG图像具有10KB的平均值,不认为他们是问题的原因(因为他们是这个小),即使这个问题开始后,我改变了ImageButtons这些PNG图像。

老实说,我不知道从哪里重新调整图像开始分开,因为他们的设计有点大的像素尺寸(而不是存储),以适应不同的设备。但我认为还有另一种解决这个问题,我作为一个初学者想不通。我希望有人能够帮助我解决这个问题:)

下面是code:

清单。

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.letsfly.tryp
    安卓版code =1
    机器人:=的versionName1.0>    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =18/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme
        机器人:可调试=真正的>
        <活动
            机器人:名字=泼水节
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@安卓风格/ Theme.NoTitleBar.Fullscreen>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@安卓风格/ Theme.NoTitleBar.Fullscreen>
            &所述;意图滤光器>
                <作用机器人:名字=com.letsfly.tryp.MAINACTIVITY/>
                <类机器人:名字=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:测试NAME =
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@安卓风格/ Theme.NoTitleBar.Fullscreen>
            &所述;意图滤光器>
                <作用机器人:名字=com.letsfly.tryp.TESTING/>
                <类机器人:名字=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=。trypOne
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=com.letsfly.tryp.TRYPONE/>
                <类机器人:名字=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=。trypTwo
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=com.letsfly.tryp.TRYPTWO/>
                <类机器人:名字=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=。trypThree
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=com.letsfly.tryp.TRYPTHREE/>
                <类机器人:名字=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=。trypFour
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=com.letsfly.tryp.TRYPFOUR/>
                <类机器人:名字=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:trypFiveNAME =
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=com.letsfly.tryp.TRYPFIVE/>
                <类机器人:名字=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>
< /清单>

开机XML。

 <?XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直    >        < ImageView的
            机器人:ID =@ + ID / imageView1
            机器人:layout_width =600dp的
            机器人:layout_height =800DP
            机器人:layout_gravity =中心
            机器人:layout_marginTop = - 180像素
            机器人:contentDescription =@字符串/按钮1
            机器人:填充=0像素
            机器人:SRC =@绘制/ trypsplash/>    < / LinearLayout中>

飞溅的Java。

 进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Window;
进口android.view.WindowManager;公共类飞溅延伸活动{
    @覆盖
    保护无效的onCreate(捆绑waitFiveSeconds){
        // TODO自动生成方法存根        requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);        super.onCreate(waitFiveSeconds);
        的setContentView(R.layout.splash);        螺纹定时器=新的Thread(){
            公共无效的run(){
                尝试{
                    睡眠(5000);
                }赶上(InterruptedException的E){
                    e.printStackTrace();                }最后{
                    意向openMenu =新意图(com.letsfly.tryp.MAINACTIVITY);
                        startActivity(openMenu);                }
            }
        };
        timer.start();
    }

}

MainActivity的Java(其中按钮)。

 包com.letsfly.tryp;进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
进口android.view.Menu;
进口android.view.View;
进口android.view.Window;
进口android.view.WindowManager;
进口android.widget.ImageButton;公共类MainActivity延伸活动{    的ImageButton按钮1;
    的ImageButton按钮2;
    的ImageButton按钮3;
    的ImageButton将Button4;
    的ImageButton button5;
    的ImageButton button6;
    的ImageButton button7;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        //用自己的活动取代yourActivity.this或者如果你声明的情况下,你可以写context.getSystemService(Context.VIBRATOR_SERVICE);        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        BUTTON1 =(的ImageButton)findViewById(R.id.imageButton1);
        按钮2 =(的ImageButton)findViewById(R.id.imageButton2);
        按钮3 =(的ImageButton)findViewById(R.id.imageButton3);
        将Button4 =(的ImageButton)findViewById(R.id.imageButton4);
        button5 =(的ImageButton)findViewById(R.id.imageButton5);
        button6 =(的ImageButton)findViewById(R.id.imageButton6);
        button7 =(的ImageButton)findViewById(R.id.imageButton7);
        button1.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                意图goToTrypOne =新意图(com.letsfly.tryp.TRYPONE);
                startActivity(goToTrypOne);            }
        });
        button2.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                意图goToTrypTwo =新意图(com.letsfly.tryp.TRYPTWO);
                startActivity(goToTrypTwo);
            }
        });
        button3.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                意图goToTrypThree =新意图(com.letsfly.tryp.TRYPTHREE);
                startActivity(goToTrypThree);            }
        });
        button4.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                意图goToTrypTwo =新意图(com.letsfly.tryp.TRYPFOUR);
                startActivity(goToTrypTwo);            }
        });
        button5.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                意图goToTrypFive =新意图(com.letsfly.tryp.TRYPFIVE);
                startActivity(goToTrypFive);            }
        });
        button6.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根            }        });
        button7.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根            }        });    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }}

MainActivity XML。

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:比重=中心
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直    工具:上下文=MainActivity。>   <的ImageButton
        机器人:ID =@ + ID / imageButton1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:contentDescription =@字符串/按钮1
        机器人:SRC =@绘制/按钮1
        机器人:layout_weight =1
        机器人:layout_gravity =中心/>
    <的ImageButton
        机器人:ID =@ + ID / imageButton2
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:contentDescription =@字符串/按钮2
        机器人:SRC =@绘制/按钮2
        机器人:layout_weight =1
        机器人:layout_gravity =中心/>
    <的ImageButton
        机器人:ID =@ + ID / imageButton3
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:contentDescription =@字符串/按钮3
        机器人:SRC =@绘制/按钮3
        机器人:layout_weight =1
        机器人:layout_gravity =中心/>
    <的ImageButton
        机器人:ID =@ + ID / imageButton4
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:contentDescription =@字符串/将Button4
        机器人:SRC =@绘制/将Button4
        机器人:layout_weight =1
        机器人:layout_gravity =中心/>
    <的ImageButton
        机器人:ID =@ + ID / imageButton5
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@绘制/ button5
        机器人:contentDescription =@字符串/ button5
        机器人:layout_weight =1
        机器人:layout_gravity =中心/>
    <的ImageButton
        机器人:ID =@ + ID / imageButton6
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:contentDescription =@字符串/ button6
        机器人:SRC =@绘制/ button6
        机器人:layout_weight =1
        机器人:layout_gravity =中心/>
    <的ImageButton
        机器人:ID =@ + ID / imageButton7
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:contentDescription =@字符串/ button7
        机器人:SRC =@绘制/ button7
        机器人:layout_weight =1
        机器人:layout_gravity =中心/>< / LinearLayout中>


解决方案

为像素的超大实际上是问题,因为Android将需要在COM pressed PNG格式膨胀到uncom pressed位图在内存中显示它们。

而不是使该缩减,以适应不同尺寸/密度的设备,而是使用文件夹绘制不同预选赛大png格式的:

 适用于mdpi设备图片:/ RES /绘,MDPI
对华电国际设备图片:/ RES /绘,华电国际
对于xhdpi设备图片:/ RES /绘,xhdpi
对于xxhdpi设备图片:/ RES /绘,xxhdpi

当您请求的图像(例如,R.drawable.myImage),它会寻找那些具有最的设备相匹配的密度预选赛文件夹中的文件MYIMAGE。这可让您自动确保中,小密度的设备正在使用的影像具有更小的内存占用。

点击此处了解详情:
http://developer.android.com/guide/practices/screens_support.html

I am working in Eclipse on an android app which starts with a splash screen (suspecting Memory Leak here after some research done). The splash screen goes into the main menu consisting of just 7 buttons which lead to some Java PApplet. The whole app was working perfectly until I changed the 7 dummy (ImageButton) png images to the finalised 7 png images. These png images have an average of 10KB and don't think they are the cause of the problem (since they're this small), even though this problem started after I changed these png images of the ImageButtons.

Honestly I don't know where to start apart from resizing the images again, cause they are designed a bit large as size in pixels (not memory) to fit different devices. But I think there is another solution to this problem which I as a beginner cannot figure out. I hope someone can help me with this problem :)

Here is the code:

Manifest.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.letsfly.tryp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:debuggable="true" > 
        <activity
            android:name=".Splash"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="com.letsfly.tryp.MAINACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Testing"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="com.letsfly.tryp.TESTING" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".trypOne"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.letsfly.tryp.TRYPONE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".trypTwo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.letsfly.tryp.TRYPTWO" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".trypThree"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.letsfly.tryp.TRYPTHREE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".trypFour"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.letsfly.tryp.TRYPFOUR" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".trypFive"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.letsfly.tryp.TRYPFIVE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>


</manifest>

Splash XML.

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

    >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="600dp"
            android:layout_height="800dp"
            android:layout_gravity="center"
            android:layout_marginTop="-180px"
            android:contentDescription="@string/button1"
            android:padding="0px"
            android:src="@drawable/trypsplash" />

    </LinearLayout>

Splash Java.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Splash extends Activity {


    @Override
    protected void onCreate(Bundle waitFiveSeconds) {
        // TODO Auto-generated method stub

        requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(waitFiveSeconds);
        setContentView(R.layout.splash);

        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                }catch(InterruptedException e){
                    e.printStackTrace();

                }finally{
                    Intent openMenu = new 

Intent("com.letsfly.tryp.MAINACTIVITY");
                        startActivity(openMenu);

                }
            }
        };
        timer.start();
    }

}

MainActivity Java (where the buttons are).

package com.letsfly.tryp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class MainActivity extends Activity {

    ImageButton button1;
    ImageButton button2;
    ImageButton button3;
    ImageButton button4;
    ImageButton button5;
    ImageButton button6;
    ImageButton button7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {




        //replace yourActivity.this with your own activity or if you declared a context you can write context.getSystemService(Context.VIBRATOR_SERVICE);    

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button1 = (ImageButton) findViewById(R.id.imageButton1);
        button2 = (ImageButton) findViewById(R.id.imageButton2);
        button3 = (ImageButton) findViewById(R.id.imageButton3);
        button4 = (ImageButton) findViewById(R.id.imageButton4);
        button5 = (ImageButton) findViewById(R.id.imageButton5);
        button6 = (ImageButton) findViewById(R.id.imageButton6);
        button7 = (ImageButton) findViewById(R.id.imageButton7);




        button1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent goToTrypOne = new Intent("com.letsfly.tryp.TRYPONE");
                startActivity(goToTrypOne);

            }
        });
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent goToTrypTwo = new Intent("com.letsfly.tryp.TRYPTWO");
                startActivity(goToTrypTwo);


            }
        });
        button3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent goToTrypThree = new Intent("com.letsfly.tryp.TRYPTHREE");
                startActivity(goToTrypThree);

            }
        });
        button4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent goToTrypTwo = new Intent("com.letsfly.tryp.TRYPFOUR");
                startActivity(goToTrypTwo);

            }
        });
        button5.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent goToTrypFive = new Intent("com.letsfly.tryp.TRYPFIVE");
                startActivity(goToTrypFive);

            }
        });
        button6.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }

        });
        button7.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }

        });

    }


    @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;
    }

}

MainActivity XML.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"

    tools:context=".MainActivity" >

   <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/button1"
        android:src="@drawable/button1"
        android:layout_weight="1"
        android:layout_gravity="center"/>
    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/button2"
        android:src="@drawable/button2"
        android:layout_weight="1"
        android:layout_gravity="center" />
    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/button3"
        android:src="@drawable/button3"
        android:layout_weight="1"
        android:layout_gravity="center" />
    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/button4"
        android:src="@drawable/button4"
        android:layout_weight="1"
        android:layout_gravity="center" />
    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/button5"
        android:contentDescription="@string/button5"
        android:layout_weight="1"
        android:layout_gravity="center" />
    <ImageButton
        android:id="@+id/imageButton6"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/button6"
        android:src="@drawable/button6"
        android:layout_weight="1"
        android:layout_gravity="center" />
    <ImageButton
        android:id="@+id/imageButton7"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/button7"
        android:src="@drawable/button7"
        android:layout_weight="1"
        android:layout_gravity="center" />

</LinearLayout>

解决方案

Being large in pixels actually is the issue, as Android will need to inflate the compressed pngs into uncompressed bitmaps in memory to display them.

Instead of making large pngs that scale down to accomodate different size/density devices, instead use drawable folders with different qualifiers:

Images for mdpi devices: /res/drawable-mdpi
Images for hdpi devices: /res/drawable-hdpi
Images for xhdpi devices: /res/drawable-xhdpi
Images for xxhdpi devices: /res/drawable-xxhdpi

When you request an image (say, R.drawable.myImage), it will look for a myImage file in the folder that has the density qualifier that best matches the device. This lets you automatically make sure that small density devices are using images with a smaller memory footprint.

More information here: http://developer.android.com/guide/practices/screens_support.html

这篇关于出内存:加入10KB ImageButtons后堆大小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆