问题
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 Applet. The whole app was working perfectly until I changed the 7 dummy (ImageButton) png images to the finalized 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>
回答1:
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
来源:https://stackoverflow.com/questions/21262919/out-of-memory-heap-size-error-after-adding-10kb-imagebuttons