InflateException: Binary XML file line #1: Error inflating class <unknown> 由 OutOfMemoryError

Posted

技术标签:

【中文标题】InflateException: Binary XML file line #1: Error inflating class <unknown> 由 OutOfMemoryError 引起【英文标题】:InflateException: Binary XML file line #1: Error inflating class <unknown> caused by OutOfMemoryError 【发布时间】:2014-01-07 22:45:08 【问题描述】:

这是我的代码:

input.xml(布局文件夹)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:context=".MainActivity"
    android:background="@drawable/background_main" >

    <ImageView 
        android:id="@+id/logo_image"
        android:background="@drawable/background_green"
        android:src="@drawable/titleimage"
        android:layout_
        android:layout_
        />

    <ScrollView
        android:id="@+id/scroller"
        android:layout_
        android:layout_
        android:layout_below="@id/logo_image"
        android:layout_marginTop="10dp"
        >

        <TableLayout
            android:layout_
            android:layout_
            android:stretchColumns="*" >

            <TableRow
                android:id="@+id/device_type_row"
                android:layout_
                android:layout_
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:layout_marginRight="5dp" >

                <RadioButton
                    android:id="@+id/device_type_radio"
                    android:layout_
                    android:layout_
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/device_type" />

                <RadioGroup 
                    android:id="@+id/device_type_radio_selection"
                    android:layout_
                    android:layout_
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/radioios"
                        android:layout_
                        android:layout_height = "wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="@string/iOS"
                        />

                    <RadioButton
                        android:id="@+id/radioAndroid"
                        android:layout_
                        android:layout_height = "wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="@string/android"
                        />


                </RadioGroup>


            </TableRow>

            <TableRow
                android:id="@+id/days_as_customers_row"
                android:layout_
                android:layout_
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:layout_marginRight="5dp" >

                <RadioButton
                    android:id="@+id/days_as_customers_radio"
                    android:layout_
                    android:layout_
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/days_as_customers" />

                <LinearLayout
                    android:layout_
                    android:layout_
                    android:weightSum="2" >

                    <SeekBar
                        android:id="@+id/days_as_customer_seekbar"
                        android:layout_
                        android:layout_
                        android:layout_weight="1" />

                    <TextView
                        android:id="@+id/days_as_customer_text"
                        android:layout_
                        android:layout_
                        android:text="1/210"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:textAppearance="?android:attr/textAppearanceSmall" />

                </LinearLayout>

            </TableRow>

            <TableRow
                android:id="@+id/average_sessions_row"
                android:layout_
                android:layout_ >

                <RadioButton
                    android:id="@+id/average_sessions"
                    android:layout_
                    android:layout_
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/average_sessions" />

                <LinearLayout
                    android:layout_
                    android:layout_
                    android:weightSum="2" >

                    <SeekBar
                        android:id="@+id/average_sessions_seekbar"
                        android:layout_
                        android:layout_
                        android:layout_weight="1" />

                    <TextView
                        android:id="@+id/average_sessions_text"
                        android:layout_
                        android:layout_
                        android:text="240/9000"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:textAppearance="?android:attr/textAppearanceSmall" />

                </LinearLayout>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow4"
                android:layout_
                android:layout_ >

                <RadioButton
                    android:id="@+id/conversion_time_radio"
                    android:layout_
                    android:layout_
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/conversion_title" />

                <LinearLayout
                    android:layout_
                    android:layout_
                    android:weightSum="2" >

                    <SeekBar
                        android:id="@+id/conversion_time_seekbar"
                        android:layout_
                        android:layout_
                        android:layout_weight="1" />

                    <TextView
                        android:id="@+id/conversion_time_text"
                        android:layout_
                        android:layout_
                        android:text="3/55"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:textAppearance="?android:attr/textAppearanceSmall" />

                </LinearLayout>

            </TableRow>

            <TableRow
                android:id="@+id/tableRow5"
                android:layout_
                android:layout_ >

                <RadioButton
                    android:id="@+id/monthly_spend_radio"
                    android:layout_
                    android:layout_
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/monthly_spend" />

                <LinearLayout
                    android:layout_
                    android:layout_
                    android:weightSum="2" >

                    <SeekBar
                        android:id="@+id/monthly_spend_seekbar"
                        android:layout_
                        android:layout_
                        android:layout_weight="1" />

                    <TextView
                        android:id="@+id/monthly_spend_text"
                        android:layout_
                        android:layout_
                        android:text="3/55"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:textAppearance="?android:attr/textAppearanceSmall" />

                </LinearLayout>

            </TableRow>

            <TableRow
                android:id="@+id/tableRow6"
                android:layout_
                android:layout_ >

                <RadioButton
                    android:id="@+id/virality_invites_sent_radio"
                    android:layout_
                    android:layout_
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/virality_invites_sent" />

                <LinearLayout
                    android:layout_
                    android:layout_
                    android:weightSum="2" >

                    <SeekBar
                        android:id="@+id/virality_invites_sent_seekbar"
                        android:layout_
                        android:layout_
                        android:layout_weight="1" />

                    <TextView
                        android:id="@+id/virality_invites_sent_text"
                        android:layout_
                        android:layout_
                        android:text="0-50"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:textAppearance="?android:attr/textAppearanceSmall" />

                </LinearLayout>



            </TableRow>

            <TableRow
                android:id="@+id/tableRow7"
                android:layout_
                android:layout_ >

                <RadioButton
                    android:id="@+id/churned_users_radio"
                    android:layout_
                    android:layout_
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/churned_users" />

                <RadioGroup 
                    android:id="@+id/churned_users_radio_selection"
                    android:layout_
                    android:layout_
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/radioYes"
                        android:layout_
                        android:layout_height = "wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="@string/iOS"
                        />

                    <RadioButton
                        android:id="@+id/radioNo"
                        android:layout_
                        android:layout_height = "wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="@string/android"
                        />


                </RadioGroup>

            </TableRow>

            <TableRow
                android:id="@+id/tableRow8"
                android:layout_
                android:layout_ >

                <RadioButton
                    android:id="@+id/ltv_total_spent_radio"
                    android:layout_
                    android:layout_
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/ltv_total_spent" />

                <LinearLayout
                    android:layout_
                    android:layout_
                    android:weightSum="2" >

                    <SeekBar
                        android:id="@+id/ltv_total_spent_seekbar"
                        android:layout_
                        android:layout_
                        android:layout_weight="1" />

                    <TextView
                        android:id="@+id/ltv_total_spent_text"
                        android:layout_
                        android:layout_
                        android:text="0$-80$"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:textAppearance="?android:attr/textAppearanceSmall" />

                </LinearLayout>

            </TableRow>
        </TableLayout>

    </ScrollView>

    <Button
        android:id="@+id/input_submit_button"
        android:layout_
        android:layout_
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/submit_button_img" />

</RelativeLayout>

InputFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class InputFragment extends Fragment 

    private Button submit;
    private RadioButton deviceTypeRadio, daysAsCustomerRadio, averageSessionsRadio, conversionTimeRadio;
    private RadioButton monthlySpendRadio,viralityInvitesSentRadio, churnedUsersRadio, LTVTotalSpentRadio; 
    private SeekBar daysAsCustomerSeek, averageSessionSeek, conersionTimeSeek, monthlySpendSeek;
    private SeekBar viralityInvitesSeek, ltvTotalSpentSeek;
    private TextView daysasAsCustomerText, averageSessionText, conversionTimeText, monthlySpendText;
    private TextView viralityInvitesText, letvTotalText;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    
        //Intializing instance variables
        View view = inflater.inflate(R.layout.input, container,false);

        submit = (Button)view.findViewById(R.id.input_submit_button);

        return view;

    


我可以正常运行这段代码,但是当我改变模拟器的方向时,它给出了以下错误。

12-19 11:17:53.590: E/AndroidRuntime(5644): FATAL EXCEPTION: main
12-19 11:17:53.590: E/AndroidRuntime(5644): java.lang.RuntimeException: Unable to start activity ComponentInfocom.xxx.xxx/com.nFlate.xxx.MainActivity: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.os.Looper.loop(Looper.java:137)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at java.lang.reflect.Method.invokeNative(Native Method)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at java.lang.reflect.Method.invoke(Method.java:511)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at dalvik.system.NativeStart.main(Native Method)
12-19 11:17:53.590: E/AndroidRuntime(5644): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at com.nFlate.nflate.InputFragment.onCreateView(InputFragment.java:30)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.app.Activity.performStart(Activity.java:5114)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
12-19 11:17:53.590: E/AndroidRuntime(5644):     ... 12 more
12-19 11:17:53.590: E/AndroidRuntime(5644): Caused by: java.lang.reflect.InvocationTargetException
12-19 11:17:53.590: E/AndroidRuntime(5644):     at java.lang.reflect.Constructor.constructNative(Native Method)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
12-19 11:17:53.590: E/AndroidRuntime(5644):     ... 27 more
12-19 11:17:53.590: E/AndroidRuntime(5644): Caused by: java.lang.OutOfMemoryError
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.content.res.Resources.loadDrawable(Resources.java:1965)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.view.View.<init>(View.java:3330)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.view.View.<init>(View.java:3259)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.view.ViewGroup.<init>(ViewGroup.java:425)
12-19 11:17:53.590: E/AndroidRuntime(5644):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:210)
12-19 11:17:53.590: E/AndroidRuntime(5644):     ... 30 more

基本上,错误是针对以下地方:

View view = inflater.inflate(R.layout.input, container,false);

这只发生在模拟器中,不会发生在手机中。这里有什么问题?

【问题讨论】:

【参考方案1】:

检查您的drawable/background_main,即您的根布局的背景图像 - 解码位图时虚拟机内存不足。缩小图片尺寸。

【讨论】:

您好,您的意思是减小位图大小吗? 是的,通过减小图像尺寸(即分辨率)来减小位图大小。 这个答案对我有帮助。谢谢:) 有同样的问题,以防万一,我的 img 大小约为 2mb,大小约为 4000x3000 px 这个答案对我有帮助..实际上我正在使用圆角的可绘制资源导致我的应用程序崩溃..使用这个答案我得到了一个线索并找到了确切的问题【参考方案2】:

我的问题是我有一个带有&lt;solid&gt; 和颜色?selectableItemBackground 的形状。是的,这很愚蠢,而且是一个具体案例,但在这里发布给有同样错误的人。

【讨论】:

【参考方案3】:

出现问题是因为 *drawable/background_main* 具有高分辨率图像。因此,当它加载时,VM 内存不足

所以最好的方法是减小它的大小或分辨率。

【讨论】:

【参考方案4】:

您在片段中使用此布局,而不是在 Activity 中。还有另一个异常 OutOfMemory 您的背景图像非常大,请尝试缩小它..

【讨论】:

【参考方案5】:

原因:java.lang.OutOfMemoryError:位图大小超出 VM 预算可能是这里真正的罪魁祸首。尝试使用较小的图像来查看它是否是与内存相关的问题。 查看这些链接以获取类似问题的示例:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget - android - how many images? 和 OutOfMemory exception when loading bitmap from external storage

【讨论】:

【参考方案6】:

对于那些有这个问题的人:有些情况可能会导致应用程序崩溃。这些是我见过最多的。

1- 使用 ?selectableItemBackground 可能会导致此问题。

2- 如果使用大图像(或其他)导致应用程序错误(例如无法分配)...这在硬件加速开启时会发生。在这种情况下,您可以将其设置为关闭,并在清单(应用程序元素)中使用大堆

3- 如果您有一个在 v-21(例如)分支中的可绘制对象,并且您在 21 以下运行应用程序,它也可能导致此问题。

祝你好运!

https://stacklearn.ir

【讨论】:

【参考方案7】:

我通过减少所有 xml 文件中 .png 按钮的尺寸解决了同样的问题

【讨论】:

【参考方案8】:

尝试使用不同分辨率的图像,例如 mdpi、hdpi、xhdpi,如果只使用更高分辨率的图像,可能会导致低分辨率手机崩溃

【讨论】:

【参考方案9】:

好吧,就我而言,答案是在 xml 设计中,冲突 android:backgroundTint 和 android:tint

我设计了一个这样的 FloatinActionButton:

<android.support.design.widget.FloatingActionButton
                    android:id="@+id/fa_close_patient"
                    android:layout_
                    android:layout_
                    android:scaleX="0.8"
                    android:scaleY="0.8"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="right"
                    android:layout_marginEnd="30dp"
                    android:backgroundTint="@color/white"
                    android:src="@drawable/ic_close_black_24dp"
                    android:tint="@color/colorPrimaryDark" />

没关系,但 API > 23

如果你为 API_LEVEL

<android.support.design.widget.FloatingActionButton
                    android:id="@+id/fa_close_patient"
                    android:layout_
                    android:layout_
                    android:scaleX="0.8"
                    android:scaleY="0.8"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="right"
                    android:layout_marginEnd="30dp"
                    app:backgroundTint="@color/white"
                    android:src="@drawable/ic_close_black_24dp"/>

希望对你有帮助,

问候

【讨论】:

【参考方案10】:

在我的情况下,我遇到了不合逻辑的类膨胀异常,经过一段时间的挖掘,我发现该错误是由于与 android 框架本身相关的已弃用代码块引起的。

【讨论】:

【参考方案11】:

在我的情况下,我使用的是可绘制的

drawable-v24

当我把它移到

可绘制

它对我有用。

【讨论】:

以上是关于InflateException: Binary XML file line #1: Error inflating class <unknown> 由 OutOfMemoryError 的主要内容,如果未能解决你的问题,请参考以下文章

如何解决此错误:InflateException: Binary XML file line #11: Error inflating class fragment

尝试为内部列表视图的每一行膨胀不同的视图 (android.view.InflateException: Binary XML..)

原因:android.view.InflateException: Binary XML file line #11: Error inflating class fragment

InflateException: Binary XML file line #1: Error inflating class <unknown> 由 OutOfMemoryError

android.view.InflateException: Binary XML file line #7: Error inflating class

android.view.InflateException Binary xml 文件第 11 行:错误膨胀类片段