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