0

单击按钮时,我只需加载一个视图翻转器 xml。此 xml 在所有设备上运行良好,但在 Google Nexus One 设备上报告崩溃。有时它运行时不会崩溃。下面是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/object_mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/obj_top_bar_RL"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip" >

        <Button
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:background="@drawable/home" />

        <Button
            android:id="@+id/sound"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/sound" />

        <Button
            android:id="@+id/settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/setting" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/obj_bottom_bar_RL"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="25dip"
            android:background="@drawable/next" />

        <Button
            android:id="@+id/prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="25dip"
            android:background="@drawable/previous" />
         <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/prev" >

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <com.google.ads.AdView
                    android:id="@+id/adView"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    ads:adSize="BANNER"
                    ads:adUnitId="a1504ac5c4238ef"
                    ads:loadAdOnCreate="true" />
            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/obj_bottom_bar_RL"
        android:layout_below="@id/obj_top_bar_RL"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >

        <ViewFlipper
            android:id="@+id/flipper_object"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/object_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/object_1" />

            <ImageView
                android:id="@+id/object_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/object_2" />

            <ImageView
                android:id="@+id/object_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/object_3" />
        </ViewFlipper>
    </FrameLayout>

</RelativeLayout>

日志如下:

Failed to inflate
android.view.InflateException: Binary XML file line #118: Error inflating class <unknown>
android.view.LayoutInflater.createView(LayoutInflater.java:518)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
android.view.LayoutInflater.inflate(LayoutInflater.java:408)
android.view.LayoutInflater.inflate(LayoutInflater.java:320)
android.view.LayoutInflater.inflate(LayoutInflater.java:276)
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:212)
android.app.Activity.setContentView(Activity.java:1657)
com.flash.object.ObjectActivity.onCreate(ObjectActivity.java:78)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
android.app.ActivityThread.access$1500(ActivityThread.java:123)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:123)
android.app.ActivityThread.main(ActivityThread.java:3839)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
dalvik.system.NativeStart.main(Native Method)
10-09 15:58:25.201: Caused by: java.lang.reflect.InvocationTargetException
java.lang.reflect.Constructor.constructNative(Native Method)
java.lang.reflect.Constructor.newInstance(Constructor.java:415)
android.view.LayoutInflater.createView(LayoutInflater.java:505)
10-09 15:58:25.201:     ... 24 more
10-09 15:58:25.201: Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
android.content.res.Resources.loadDrawable(Resources.java:1782)
android.content.res.TypedArray.getDrawable(TypedArray.java:601)
android.view.View.<init>(View.java:1967)
android.widget.ImageView.<init>(ImageView.java:112)
android.widget.ImageView.<init>(ImageView.java:108)
10-09 15:58:25.201:     ... 27 more

请让我知道是否有人可以建议我..!!! :(

4

2 回答 2

0

利用

android:layout_below="@+id/prev

始终使用“@+id/yourId”

于 2012-10-09T10:53:56.040 回答
0

从 Stack Overflow 的一篇文章中得到了我的 OutOfMemoryException 的解决方案。是的,我已将该解决方案标记为向上箭头。

@Override
    protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.RootView));
    System.gc();
    }

    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));
            }
        ((ViewGroup) view).removeAllViews();
        }
    }

在 Activity 的 onDestroy() 方法上,调用 unbindDrawables() 方法传递对父视图的引用,然后执行 System.gc()。

这个 unbindDrawables() 方法递归地探索视图树并且:

删除所有背景可绘制对象上的回调删除每个视图组上的子项

于 2012-10-09T12:19:58.803 回答