应用程序崩溃,因为它需要太多内存?

时间:2014-01-28 11:25:56

标签: android memory crash admob

我即将完成我的应用程序,最近我遇到了一个无法修复的问题。 我的应用程序包含5个布局,每个布局有1个活动,5个布局。 从我向我的应用程序添加广告以来,我的应用程序声称在ram内存上占用了大量的内容,在切换几个布局后它崩溃了。 我的应用程序大小为10.8mb,但在手机上它需要高达75mb的Ram内存。 这是我从logcat获得的警告:http://prntscr.com/2naqr8 当应用程序崩溃时,这是Logcat:http://prntscr.com/2naret第二部分:http://prntscr.com/2narhi第三部分:http://prntscr.com/2narhi

现在我的代码是非常简单的代码,第一个活动是启动画面,第二个是菜单,其他3个是相同的代码。 菜单代码:

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();

    }
});


}

此后,它会重复2次以进行其他2项活动。 我的其他3项活动是相同的,只有资源不同。

    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();
     }
 }

就是这样,整个代码我解开所有drawables,释放MP和活动,并且仍然在Device和crashes上占用太多空间。有谁知道这可能导致什么? 最后,这是我的XML,同样,除了IDS和资源

之外,所有布局都是一样的
<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>

1 个答案:

答案 0 :(得分:3)

通常在加载大位图时会发生此错误。 ImageButtons的绘图是否具有高分辨率?如果是这样,这可能是错误。您应该尝试将它们降低到适当的分辨率,但是为了快速修复,在AndroidManifest.xml文件的android:largeHeap="true"标记下添加<application>可以允许您的应用加载大型图像而不会出现内存不足错误。

如果以上都不起作用,那么问题就来自代码中的其他地方。但从个人经验来看,位图最容易导致简单的代码因内存问题而崩溃。

相关问题