开发者

Why am I getting memory leakage when i am simpy doing setContentView(R.layout.somelayout)?

in my onCreate(), i am simply using an xml as a content view for the activity. why am i getting a memory leak. is it something i am doing wrong?

My xml simply has 5 image buttons, with a statelistdrawable (selector) for each imagebutton. each image is just 16kb in size. (So i am using 10 images- a pressed state image and an normal state image for each button).

Nowhere in my activity am i using LayoutInflator.. Shouldn't android be recycling bimaps on its own when setting the content view from xml?

I am getting error something like this:

02-17 09:18:39.797: ERROR/AndroidRuntime(372): Uncaught handler: thread main exiting due to uncaught exception
02-17 09:18:39.817: ERROR/AndroidRuntime(372): java.lang.RuntimeException: Unable to start activity ComponentInfo{***.***.*****.activity/***.***.*****.home.HomeScreenActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class java.lang.reflect.Constructor
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.os.Looper.loop(Looper.java:123)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.app.ActivityThread.main(ActivityThread.java:4203)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at java.lang.reflect.Method.invokeNative(Native Method)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at java.lang.reflect.Method.invoke(Method.java:521)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at dalvik.system.NativeStart.main(Native Method)
02-17 09:18:39.817: ERROR/AndroidRuntime(372): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class java.lang.reflect.Constructor
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.view.LayoutInflater.createView(LayoutInflater.java:512)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.app.Activity.setContentView(Activity.java:1620)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at ***.***.*****.home.HomeScreenActivity.onCreate(HomeScreenActivity.java:35)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     ... 11 more
02-17 09:18:39.817: ERROR/AndroidRuntime(372): Caused by: java.lang.reflect.InvocationTargetException
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:171)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at java.lang.reflect.Constructor.constructNative(Native Method)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.view.LayoutInflater.createView(LayoutInflater.java:499)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     ... 21 more
02-17 09:18:39.817: ERROR/AndroidRuntime(372): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:439)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:322)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:688)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.content.res.Resources.loadDrawable(Resources.java:1710)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.content.res.Typed开发者_如何转开发Array.getDrawable(TypedArray.java:548)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.view.View.<init>(View.java:1846)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.view.View.<init>(View.java:1795)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     at android.view.ViewGroup.<init>(ViewGroup.java:282)
02-17 09:18:39.817: ERROR/AndroidRuntime(372):     ... 25 more

I replaced the images, and the code worked without the application hanging up, but stikll my ddms log was going crazy with the following errors:

02-17 09:18:37.287: ERROR/(372): VM won't let us allocate 270000 bytes
02-17 09:18:37.287: ERROR/dalvikvm-heap(372): 270000-byte external allocation too large for this process.
02-17 09:18:37.287: ERROR/(372): VM won't let us allocate 270000 bytes
02-17 09:18:37.287: ERROR/dalvikvm-heap(372): 270000-byte external allocation too large for this process.
02-17 09:18:37.287: ERROR/(372): VM won't let us allocate 270000 bytes
02-17 09:18:37.287: ERROR/dalvikvm-heap(372): 270000-byte external allocation too large for this process.
02-17 09:18:37.287: ERROR/(372): VM won't let us allocate 270000 bytes
02-17 09:18:37.287: ERROR/dalvikvm-heap(372): 264600-byte external allocation too large for this process.

.. . . . .

Although the app is working, i would like to know what i am doing wrong. or is it a bug in the buggy android system?

attaching the code:

public class VirtualHomeActivity extends Activity  {

    ImageButton imgbtn1, imgbtn2, imgbtn3, imgbtn4,
            imgbtn5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
}
}

layout home.xml:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layoutAnimation="@anim/home_screen_animation_controller" android:background="@drawable/bg_application">

    <ImageButton android:id="@+id/imgBtn1"
        android:layout_width="340dip" android:scaleType="fitCenter" android:layout_height="100dip" android:layout_marginLeft="20dip" android:layout_marginTop="5dip" android:background="@drawable/custombutton1"></ImageButton>
    <ImageButton android:layout_below="@id/imgBtn1"
        android:id="@+id/imgBtn2" android:layout_width="340dip" android:scaleType="fitCenter" android:layout_height="100dip" android:layout_marginTop="5dip" android:layout_marginLeft="20dip" android:background="@drawable/custombutton2"></ImageButton>
    <ImageButton android:layout_below="@id/imgBtn2      android:id="@+id/imgBtn3" android:scaleType="fitCenter" android:layout_width="340dip" android:layout_height="100dip" android:layout_marginLeft="20dip" android:layout_marginTop="5dip" android:background="@drawable/custombutton3"></ImageButton>
<ImageButton android:layout_below="@id/imgBtnS3" android:id="@+id/imgBtn4" android:scaleType="fitCenter" android:layout_height="100dip" android:layout_width="340dip" android:layout_marginLeft="20dip" android:layout_marginTop="5dip" android:background="@drawable/custombutton4"></ImageButton>
<ImageButton android:layout_below="@id/imgBtn4"
    android:id="@+id/imgBtn5" android:scaleType="fitCenter"
    android:layout_height="100dip" android:layout_width="340dip"
    android:layout_marginLeft="20dip" android:layout_marginTop="5dip" android:background="@drawable/custombutton5"></ImageButton>
</RelativeLayout>

and the selector is:(custombutton1.xml)

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/img_clicked" />
    <item android:drawable="@drawable/img_unclicked" /> </selector>

i removed all other code from the activity to check whether i was creating a memory leak. But here i am jsut setting content view as my layout file, and still my ddms says VM won't let us allocate 270000 bytes

i dont think i tried to manage any activity life cycles here.


Your memory leak may be somewhere else, perhaps in how you are managing activity lifecycle changes. Take a look at the article Avoiding Memory Leaks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜