android - 应用程序崩溃是因为占用太多内存?

标签 android memory crash admob

我即将完成我的申请,但最近我遇到了一个我无法解决的问题。 我的应用程序包含 5 个布局,每个布局 1 个 Activity ,5 个布局。 现在,自从我将广告添加到我的应用程序后,我的应用程序声明在 ram 内存上占用了巨大的空间,并且在切换了几个布局后它崩溃了。 我的应用程序大小为 10.8mb,但在手机上它最多占用 75mb 的 Ram 内存。 这是我从 logcat 得到的警告:http://prntscr.com/2naqr8 这是应用程序崩溃时的 Logcat:http://prntscr.com/2naret第二部分:http://prntscr.com/2narhi第三部分:http://prntscr.com/2narhi

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

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 个 Activity 重复 2 次。 我的其他 3 个 Activity 是相同的,唯一不同的是资源。

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

就是这样,整个代码我取消绑定(bind)所有可绘制对象,释放 MP 和 Activity ,但仍然在设备上占用过多空间并崩溃。任何人都知道这可能是什么原因造成的? 最后这是我的 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>

最佳答案

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

如果以上都不起作用,那么问题出在代码的其他地方。但根据个人经验,位图最有可能导致简单代码因内存问题而崩溃。

关于android - 应用程序崩溃是因为占用太多内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21404316/

相关文章:

c - 如果循环外没有 printf,for 循环中的 Printf 将无法工作

c++ - 为什么 WinDbg 作为 C++ 应用程序崩溃的默认应用程序打开?

android - CompanionDeviceManager 'onDeviceFound' 未调用回调函数

android - 在 fragment 页面适配器中重用 fragment

java - 在 GRPC 中拦截/记录请求和响应

c++ - SSE 与类(class)保持一致

android - 有没有办法让人们的 Facebook 喜欢页面?

c++ - 检查内存值

java - 什么是 RMI TCP 连接

c - 如何调试内存随机变化问题