自定义 LinearLayout 二进制文件 XML 文件行 #1:膨胀错误 class
custom LinearLayout Binary XML file line #1: Error inflating class
我做了一个自定义的LinearLayout,xml发生了错误
此代码是 xamarin(C#) 代码,但 xml 代码类似于 Java。
编写代码。
Android.Views.InflateException: Binary XML file line #1: Error inflating class com.eappandroid.phone1.OpendCheckLinearLayout
首先,未处理的异常行是(在 LeftDrawerMenuItemAdapter class)(在 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 class
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;
}
}
}
}
感谢您的帮助
实际上,inflate 异常并不是真正的问题,但它确实来自布局中另一个更深层次的问题,然后包裹在 InflateException
中。一个常见的问题是在尝试膨胀 ImageView
加载可绘制资源时出现内存不足异常。如果其中一个资源具有高像素分辨率,则会占用大量内存,从而导致膨胀异常。
所以基本上,请验证可绘制图像中的像素分辨率是否恰好是布局所需的最低像素分辨率。它帮助了我很多次。
我希望它也对你有用。
您的命名空间似乎有误。
你把 com.eappandroid.phone1.OpendCheckLinearLayout
放在你的 xml 里,但是你的 OpendCheckLinearLayout class 你有 EAppAndroid.Protype.LeftDrawerMenu2
,所以你应该把 com.eappandroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout
编辑:
似乎xml中只需要命名空间,所以你应该把eappandroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout
我做了一个自定义的LinearLayout,xml发生了错误
此代码是 xamarin(C#) 代码,但 xml 代码类似于 Java。
编写代码。
Android.Views.InflateException: Binary XML file line #1: Error inflating class com.eappandroid.phone1.OpendCheckLinearLayout
首先,未处理的异常行是(在 LeftDrawerMenuItemAdapter class)(在 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 class
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;
}
}
}
}
感谢您的帮助
实际上,inflate 异常并不是真正的问题,但它确实来自布局中另一个更深层次的问题,然后包裹在 InflateException
中。一个常见的问题是在尝试膨胀 ImageView
加载可绘制资源时出现内存不足异常。如果其中一个资源具有高像素分辨率,则会占用大量内存,从而导致膨胀异常。
所以基本上,请验证可绘制图像中的像素分辨率是否恰好是布局所需的最低像素分辨率。它帮助了我很多次。
我希望它也对你有用。
您的命名空间似乎有误。
你把 com.eappandroid.phone1.OpendCheckLinearLayout
放在你的 xml 里,但是你的 OpendCheckLinearLayout class 你有 EAppAndroid.Protype.LeftDrawerMenu2
,所以你应该把 com.eappandroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout
编辑:
似乎xml中只需要命名空间,所以你应该把eappandroid.Protype.LeftDrawerMenu2.OpendCheckLinearLayout