应用程序崩溃,因为它需要太多的内存? [英] Application crashes because it takes too much memory?

查看:162
本文介绍了应用程序崩溃,因为它需要太多的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要完成我的应用程序,最近我遇到了,我不能修复的问题。 我的应用程序包含5个布局,和1个活动为每个布局,5布局。 现在,自从我加入广告到我的申请,我的应用程序规定采取规模巨大的RAM内存和交换一些布局崩溃之后。 我的应用程序大小为10.8MB,但在手机上花费最多的RAM内存75MB。 这是我从logcat中得到警告: http://prntscr.com/2naqr8 这是logcat的时候应用程序崩溃: http://prntscr.com/2naret 第2部分:的 http://prntscr.com/2narhi 第三部分:的 http://prntscr.com/2narhi

I am about to finish my Application and recently I encountered a problem that I can't fix. My application is contained of 5 layouts, and 1 activity for each layout, 5 layouts. Now ever since I added ads to my Application, my application stated taking enormous size on ram memory and after switching few layouts it crashes. My App size is 10.8mb but on phone it takes up to 75mb of Ram memory. Here is the warning I get from logcat: http://prntscr.com/2naqr8 Here is Logcat when Application crashes: http://prntscr.com/2naret 2nd part: http://prntscr.com/2narhi 3rd part: http://prntscr.com/2narhi

现在我的code是pretty的简单得多code,第一个活动是闪屏,第二个是菜单和其他3相同codeS。 菜单code:

Now my code is pretty much simple code, 1st activity is Splash screen, 2nd is menu and other 3 are same codes. Menu code:

package puske.com;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Menu extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    AdView myAdView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest();
    myAdView.loadAd(adRequest);




Button button1=(Button)findViewById(R.id.menu1);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        startActivity(new Intent(Menu.this, Rifles.class));

        finish();

    }
});


}

在这一点,repeats2次,另外2个活动。 我的另3项活动是一样的,唯一不同的是资源。

After this it repeats2 times for other 2 activities. My other 3 activities are same, only difference is in resources.

    package puske.com;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Pistols extends Activity {
    private SeekBar volumeSeekbar = null;
    private AudioManager audioManager = null;
    MediaPlayer mp, mp2, mp3, mp4, mp5, mp6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pistols);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        initControls(); 

        mp=MediaPlayer.create(this, R.raw.glock);
        ImageButton abt1 = (ImageButton) findViewById(R.id.abt1);
        abt1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mp2.isPlaying()){
                mp2.pause();
                mp2.seekTo(0);
            }
            if (mp3.isPlaying()){
                mp3.pause();
                mp3.seekTo(0);
            }
            if (mp4.isPlaying()){
                mp4.pause();
                mp4.seekTo(0);
            }
            if (mp5.isPlaying()){
                mp5.pause();
                mp5.seekTo(0);

            }
            if (mp6.isPlaying()){
                mp6.pause();
                mp6.seekTo(0);

            }
            if (mp.isPlaying()){
                mp.pause();
                mp.seekTo(0);
            }
            else{
                mp.start();
            }
            }

    });

  @Override
protected void onDestroy() {

    super.onDestroy();
     // explicitly release media player

    unbindDrawables(findViewById(R.id.pistolz));
    System.gc();
}

/**
 * Unbinds all drawables in a given view (and its child tree).
 * 
 * @param findViewById     Root view of the tree to unbind
 */
private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }

    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        try
        {
            ((ViewGroup) view).removeAllViews();
        }
        catch(UnsupportedOperationException ignore)
        {
            //if can't remove all view (e.g. adapter view) - no problem 
        }
    if(mp!=null){
        mp.stop();
        mp.release();
        mp = null;

}
    if(mp2!=null){
        mp2.stop();
        mp2.release();
        mp2 = null;

}
    if(mp3!=null){
        mp3.stop();
        mp3.release();
        mp3 = null;

}
    if(mp4!=null){
        mp4.stop();
        mp4.release();
        mp4 = null;

}
    if(mp5!=null){
        mp5.stop();
        mp5.release();
        mp5 = null;


}


    if(mp6!=null){
        mp6.stop();
        mp6.release();
        mp6 = null;

}
    findViewById(R.id.pistolz).setVisibility(View.GONE);




}
}
@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(Pistols.this, Menu.class));
    this.finish();
     }
 }

这就是它,整个code我解除绑定所有的可绘,放MP和活动,仍然押在设备和崩溃太多的空间。任何人都知道可能会导致什么呢? 最后,这里是我的XML,再次,它是一样的,除了IDS和资源

That's about it, whole code I unbind all drawables, release MP and activities and still stakes too much space on Device and crashes. Anyone know what might be causing this? And finally here is my XML, again, it's same for all layouts except for IDS and resources

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/black"
android:id="@+id/riflez" >
<SeekBar
    android:id="@+id/sikbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="48dp"/>
<ImageButton
    android:id="@+id/bt1"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/emka" />
<ImageButton
    android:id="@+id/bt2"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignRight="@+id/sikbar"
    android:layout_alignTop="@+id/bt1"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/kalas" />

<ImageButton
    android:id="@+id/bt3"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignLeft="@+id/bt1"
    android:layout_below="@+id/bt1"
    android:layout_marginTop="26dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/famas" />
<ImageButton
    android:id="@+id/bt4"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignLeft="@+id/bt2"
    android:layout_alignTop="@+id/bt3"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/hekler" />
<ImageButton
    android:id="@+id/bt5"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignLeft="@+id/bt3"
    android:layout_below="@+id/bt3"
    android:layout_marginTop="26dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/sporet" />
<ImageButton
    android:id="@+id/bt6"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignLeft="@+id/bt4"
    android:layout_alignTop="@+id/bt5"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/uzi" />
    <com.google.ads.AdView
    android:id="@+id/adView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="THE ID"
    ads:loadAdOnCreate="true" >
</com.google.ads.AdView>

推荐答案

装载大位图时,通常会出现此错误。是可绘制你ImageButtons高分辨率?如果是这样,这是有可能的误差。你应该尝试一下下采样,以自己合适的分辨率,但对于快速修复,添加安卓largeHeap =真正的&LT下;用途&gt; 您AndroidManifest.xml文件的标签可以让你的应用程序加载的大图像,无需内存不足的错误。

This Error occurs usually when loading large bitmaps. Are the drawables for you ImageButtons high resolution? If so, this is likely the error. You should try downsampling them to their appropriate resolution, but for a quick-fix, adding android:largeHeap="true" under the <application> tag of your AndroidManifest.xml file can allow your application to load large images without an out of memory error.

如果没有上面的工作,那么问题就从别的地方来了,在code。但是从个人的经验,位图是事情最有可能导致简单的code从内存问题崩溃。

If neither of the above work, then the problem is coming from somewhere else in the code. But from personal experience, the bitmaps are the thing most likely to cause simple code to crash from memory issues.

这篇关于应用程序崩溃,因为它需要太多的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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