【问题标题】:custom LinearLayout Binary XML file line #1: Error inflating class自定义 LinearLayout 二进制 XML 文件第 1 行:错误膨胀类
【发布时间】:2015-04-22 06:37:35
【问题描述】:

我做了一个自定义的LinearLayout,发生了xml错误
此代码是 xamarin(C#) 代码,但 xml 代码就像 Java。
编写代码。

Android.Views.InflateException: Binary XML file line #1: Error inflating class com.eappandroid.phone1.OpendCheckLinearLayout


首先,未处理的异常行是(在 LeftDrawerMenuItemAdapter 类中)(在 GetView 中)

convertView = ((Activity)Context).LayoutInflater.Inflate(Resource.Layout.LeftDrawerMenu_List_Item, null);

LeftDrawerMenu_List_Item xml

<?xml version="1.0" encoding="utf-8"?>
<com.eappandroid.phone1.OpendCheckLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="52dp"
    android:background="@drawable/menu_item_background_color_pressed"
    android:minHeight="25px"
    android:minWidth="25px"
    android:id="@+id/left_drawer_list_item_layout"
    android:orientation="horizontal">
  <ImageView
      android:id="@+id/left_drawer_list_icon"
      android:layout_width="36dp"
      android:layout_height="36dp"
      android:layout_gravity="center"
      android:layout_marginLeft="12dp"
      android:gravity="center" />
  <TextView
      android:id="@+id/left_drawer_list_item_text"
      android:layout_width="wrap_content"
      android:layout_height="52dp"
      android:gravity="left|center"
      android:layout_gravity="center"
      android:layout_marginLeft="8dp"
      android:layout_weight="1"
      android:text="item"
      android:textColor="@drawable/menu_item_title_color_pressed"
      android:textSize="16sp"
      android:textStyle="bold" />
  <ImageView
      android:id="@+id/left_drawer_noti_alert"
      android:layout_width="22dp"
      android:layout_height="22dp"
      android:layout_gravity="center"
      android:layout_marginRight="18dp"
      android:background="@drawable/menu_icon_noti_new_alert"
      android:gravity="center" />
</com.eappandroid.phone1.OpendCheckLinearLayout>


OpendCheckLinearLayout 类

namespace EAppAndroid.Protype.LeftDrawerMenu2
{


    public class OpendCheckLinearLayout : LinearLayout
    {
        private static readonly int[] STATE_MENU_OPEND = { Resource.Attribute.state_menu_opend };
        public bool menuOpen = false;



        public OpendCheckLinearLayout(Context context)
            : base(context, null)
        {

        }

        public OpendCheckLinearLayout(Context context, Android.Util.IAttributeSet attributeSet)
            : base(context, attributeSet)
        {

        }

        public OpendCheckLinearLayout(Context context, Android.Util.IAttributeSet attributeSet, int defStyle)
            : base(context, attributeSet, defStyle)
        {

        }



        protected override int[] OnCreateDrawableState(int extraSpace)
        {
            if (menuOpen)
            {
                int[] drawableStates = base.OnCreateDrawableState(extraSpace + 1);
                MergeDrawableStates(drawableStates, STATE_MENU_OPEND);

                return drawableStates;
            }
            else
            {
                return base.OnCreateDrawableState(extraSpace);
            }

        }

        public void setMenuOpen(bool menuOpen)
        {

            if (this.menuOpen != menuOpen)
            {
                this.menuOpen = menuOpen;

            }
        }
    }

}

感谢您的帮助

【问题讨论】:

  • 只有当您在小部件的背景中添加大尺寸图像时才会出现此异常。所以尝试删除所有图像然后运行。
  • @AnshulTyagi 也必须删除,但是,使用默认的 LinearLayout 时没有错误。所以,我想到了这个自定义布局问题

标签: android xml android-layout xamarin xamarin.android


【解决方案1】:

您的命名空间似乎有误。

你把com.eappandroid.phone1.OpendCheckLinearLayout放在你的xml中,但是在你的OpendCheckLinearLayout类你有EAppAndroid.Protype.LeftDrawerMenu2,所以你应该把com.eappandroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout放在里面

编辑:

xml中好像只需要命名空间,所以你应该把eappandroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout

【讨论】:

  • 嗨。我试过了。但是.. 未处理的异常:Android.Views.InflateException:二进制 XML 文件第 1 行:膨胀类 com.eappandroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout 时出错
  • 我的包名:com.eappandroid.phone1 和 OpendCheckLinearLayout src:EAppAndroid\Protype\LeftDrawerMenu2\OpendCheckLinearLayout.cs
  • 也可以试试 eappandroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout 或 com.eappandroid.phone1.eappandroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout
  • 为我工作:EAppAndroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout,非常感谢!
  • 太棒了!我将编辑我的答案,以便将来参考更清楚。
【解决方案2】:

实际上,inflate 异常并不是真正的问题,但它确实来自布局中的另一个更深层次的问题,该问题随后被包裹在 InflateException 中。一个常见的问题是在尝试为 ImageView 加载可绘制资源时出现内存不足异常。如果其中一个资源具有高像素分辨率,则会占用大量内存,从而导致 inflate 异常。

因此,基本上,验证可绘制图像中的像素分辨率是否只是布局所需的最低要求。它帮助了我很多次。

我希望它也对你有用。

【讨论】:

  • 嗨。我只需要使用最低分辨率,图像使用一种颜色(黑色)和 108x108 像素
猜你喜欢
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多