给ImageButton充气时出错

时间:2013-12-28 08:23:44

标签: android android-layout android-fragments imagebutton android-inflate

我点击Fragment-A中的ImageButton,它将变为Fragment-B。

但是如果我在Fragment-B中添加两个ImageButton,它就会崩溃。

以下代码位于Fragment-A。

ImageButton FileButton = (ImageButton) view.findViewById(R.id.FileButton) ;

        FileButton.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {

                boolean engineerMode = ((MainActivity) getActivity()).mEngineerMode ;
                    Fragment fragment = FileBrowserFragment.newInstance(null, null, null) ;
                    MainActivity.addFragment(MjpegPlayerFragment.this, fragment) ;              
            }
        }) ;

以下代码位于Fragment-A的XML文件中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000000" >


                    <Space 
                android:layout_width="0dp"
                android:layout_weight="4"
                android:layout_height="wrap_content"/>

            <ImageButton
                android:id="@+id/camera_album"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="2"
                android:background="@drawable/camera_album" 
                android:contentDescription="@string/imagebut_cmaera_album"/>

            <Space 
                android:layout_width="0dp"
                android:layout_weight="8"
                android:layout_height="wrap_content"/>

            <ImageButton
                android:id="@+id/phone_album"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="2"
                android:background="@drawable/phone_album"
                android:contentDescription="@string/imagebut_phone_album"/>

            <Space 
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_height="wrap_content"/>
        </LinearLayout>



    <TextView
        android:id="@+id/browserTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:text="@string/label_file_browser"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/browserList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/group_background"
        android:orientation="vertical" >
    </ListView>

</LinearLayout>

错误日志

D/MJPEG Player(18485): Fragment paused
D/dalvikvm(18485): GC_FOR_ALLOC freed 2732K, 16% free 50454K/59719K, paused 17ms, total 18ms
I/dalvikvm-heap(18485): Grow heap (frag case) to 62.012MB for 4194320-byte allocation
D/dalvikvm(18485): GC_FOR_ALLOC freed 1028K, 15% free 54548K/63879K, paused 16ms, total 16ms
I/dalvikvm-heap(18485): Forcing collection of SoftReferences for 4194320-byte allocation
D/dalvikvm(18485): GC_BEFORE_OOM freed 9K, 15% free 54538K/63879K, paused 27ms, total 27ms
E/dalvikvm-heap(18485): Out of memory on a 4194320-byte allocation.
I/dalvikvm(18485): "main" prio=5 tid=1 RUNNABLE
I/dalvikvm(18485):   | group="main" sCount=0 dsCount=0 obj=0x416cd6b8 self=0x400da030
I/dalvikvm(18485):   | sysTid=18485 nice=0 sched=0/0 cgrp=apps handle=1075394608
I/dalvikvm(18485):   | schedstat=( 1039401788 552784985 2517 ) utm=54 stm=49 core=0
I/dalvikvm(18485):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
I/dalvikvm(18485):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
I/dalvikvm(18485):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
I/dalvikvm(18485):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
I/dalvikvm(18485):   at android.content.res.Resources.loadDrawable(Resources.java:1935)
I/dalvikvm(18485):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
I/dalvikvm(18485):   at android.view.View.<init>(View.java:3392)
I/dalvikvm(18485):   at android.widget.ImageView.<init>(ImageView.java:114)
I/dalvikvm(18485):   at android.widget.ImageButton.<init>(ImageButton.java:87)
I/dalvikvm(18485):   at android.widget.ImageButton.<init>(ImageButton.java:83)
I/dalvikvm(18485):   at java.lang.reflect.Constructor.constructNative(Native Method)
I/dalvikvm(18485):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
I/dalvikvm(18485):   at android.view.LayoutInflater.createView(LayoutInflater.java:587)
I/dalvikvm(18485):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
I/dalvikvm(18485):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
I/dalvikvm(18485):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
I/dalvikvm(18485):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
I/dalvikvm(18485):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
I/dalvikvm(18485):   at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
I/dalvikvm(18485):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
I/dalvikvm(18485):   at tw.com.a_i_t.IPCamViewer.FileBrowser.FileBrowserFragment.onCreateView(FileBrowserFragment.java:200)
I/dalvikvm(18485):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:833)
I/dalvikvm(18485):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
I/dalvikvm(18485):   at android.app.BackStackRecord.run(BackStackRecord.java:635)
I/dalvikvm(18485):   at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1405)
I/dalvikvm(18485):   at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:441)
I/dalvikvm(18485):   at tw.com.a_i_t.IPCamViewer.MainActivity.addFragment(MainActivity.java:131)
I/dalvikvm(18485):   at tw.com.a_i_t.IPCamViewer.Viewer.MjpegPlayerFragment$4.onClick(MjpegPlayerFragment.java:1145)
I/dalvikvm(18485):   at android.view.View.performClick(View.java:4147)
I/dalvikvm(18485):   at android.view.View$PerformClick.run(View.java:17161)
I/dalvikvm(18485):   at android.os.Handler.handleCallback(Handler.java:615)
I/dalvikvm(18485):   at android.os.Handler.dispatchMessage(Handler.java:92)
I/dalvikvm(18485):   at android.os.Looper.loop(Looper.java:213)
I/dalvikvm(18485):   at android.app.ActivityThread.main(ActivityThread.java:4786)
I/dalvikvm(18485):   at java.lang.reflect.Method.invokeNative(Native Method)
I/dalvikvm(18485):   at java.lang.reflect.Method.invoke(Method.java:511)
I/dalvikvm(18485):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
I/dalvikvm(18485):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
I/dalvikvm(18485):   at dalvik.system.NativeStart.main(Native Method)
I/dalvikvm(18485): 
D/skia    (18485): --- bitmap->allocPixels failed
D/AndroidRuntime(18485): Shutting down VM
W/dalvikvm(18485): threadid=1: thread exiting with uncaught exception (group=0x416cc450)
--------- beginning of /dev/log/system

E/AndroidRuntime(18485): FATAL EXCEPTION: main
E/AndroidRuntime(18485): android.view.InflateException: Binary XML file line #31: Error inflating class <unknown>
E/AndroidRuntime(18485):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
E/AndroidRuntime(18485):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
E/AndroidRuntime(18485):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
E/AndroidRuntime(18485):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
E/AndroidRuntime(18485):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(18485):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
E/AndroidRuntime(18485):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(18485):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(18485):    at tw.com.a_i_t.IPCamViewer.FileBrowser.FileBrowserFragment.onCreateView(FileBrowserFragment.java:200)
E/AndroidRuntime(18485):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:833)
E/AndroidRuntime(18485):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
E/AndroidRuntime(18485):    at android.app.BackStackRecord.run(BackStackRecord.java:635)
E/AndroidRuntime(18485):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1405)
E/AndroidRuntime(18485):    at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:441)
E/AndroidRuntime(18485):    at tw.com.a_i_t.IPCamViewer.MainActivity.addFragment(MainActivity.java:131)
E/AndroidRuntime(18485):    at tw.com.a_i_t.IPCamViewer.Viewer.MjpegPlayerFragment$4.onClick(MjpegPlayerFragment.java:1145)
E/AndroidRuntime(18485):    at android.view.View.performClick(View.java:4147)
E/AndroidRuntime(18485):    at android.view.View$PerformClick.run(View.java:17161)
E/AndroidRuntime(18485):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(18485):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(18485):    at android.os.Looper.loop(Looper.java:213)
E/AndroidRuntime(18485):    at android.app.ActivityThread.main(ActivityThread.java:4786)
E/AndroidRuntime(18485):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(18485):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(18485):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(18485):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime(18485):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(18485): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(18485):    at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime(18485):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
E/AndroidRuntime(18485):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
E/AndroidRuntime(18485):    ... 26 more
E/AndroidRuntime(18485): Caused by: java.lang.OutOfMemoryError
E/AndroidRuntime(18485):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
E/AndroidRuntime(18485):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
E/AndroidRuntime(18485):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
E/AndroidRuntime(18485):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
E/AndroidRuntime(18485):    at android.content.res.Resources.loadDrawable(Resources.java:1935)
E/AndroidRuntime(18485):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
E/AndroidRuntime(18485):    at android.view.View.<init>(View.java:3392)
E/AndroidRuntime(18485):    at android.widget.ImageView.<init>(ImageView.java:114)
E/AndroidRuntime(18485):    at android.widget.ImageButton.<init>(ImageButton.java:87)
E/AndroidRuntime(18485):    at android.widget.ImageButton.<init>(ImageButton.java:83)
E/AndroidRuntime(18485):    ... 29 more
W/ActivityManager(  568):   Force finishing activity tw.com.a_i_t.IPCamViewer/.MainActivity

有人可以教我如何解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

布局xml

中没有ID为R.id.FileButton的ImageButton

另外

Caused by: java.lang.OutOfMemoryError

您需要缩小图像。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

您也可以查看

Out of Memory error with Bitmap

答案 1 :(得分:1)

你的崩溃与调用Activity的相对奇怪的方法无关(那是什么“addFragment方法?!?)...

...无论如何你的崩溃是因为你在给ImageView充气时内存不足:

E/dalvikvm-heap(18485): Out of memory on a 4194320-byte allocation.

你的android:background="@drawable/phone_album"可能太大(或其他一个可绘制的东西)。

帮自己一个忙,并使用Picasso加载图片。在加载图片时你可以做很多事情,最好把它留给那些有经验的人;)

答案 2 :(得分:0)

在您发布的XML布局中,我看不到ID为FileButton的ImageView。它在哪里? 至于Caused by: java.lang.OutOfMemoryError行,请看一下类似的问题:

Strange out of memory issue while loading an image to a Bitmap object

Android : java.lang.OutOfMemoryError: