复选框膨胀错误

发布于 2024-11-08 04:05:02 字数 8559 浏览 0 评论 0原文

我在 Android 市场中的应用程序中得到了奇怪的堆栈跟踪。 我有一个视图切换器,其中包含两个视图,其中一个包含一个复选框。

我得到这个堆栈跟踪 AdDetailView 是一个自定义视图。

  android.view.InflateException: Binary XML file line #29: Error inflating class android.widget.CheckBox
  at android.view.LayoutInflater.createView(LayoutInflater.java:513)
  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:382)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
  at com.yakaz.view.AdDetailView.init(AdDetailView.java:67)
  at com.yakaz.view.AdDetailView.<init>(AdDetailView.java:78)

这发生在以下视图构造函数中:

private void init(Context context) {
    LayoutInflater li = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    li.inflate(R.layout.ad_detail_view, this);
    setOrientation(LinearLayout.VERTICAL);
}

public AdDetailView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

第 67 行是充气线。

问题是我无法在模拟器或设备上重现该问题。 有什么指点吗?

这是 ad_detail_view.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/preview_warning"
    style="@style/Notification" android:text="@string/this_is_only_a_preview"
    android:visibility="gone" />

<ScrollView android:layout_width="fill_parent"
    android:fadingEdge="none" android:layout_height="fill_parent"
    android:layout_weight="1" android:fillViewport="true" android:id="@+id/scrolls">
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:orientation="vertical">

        <include layout="@layout/action_bar" />

        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:paddingTop="10dp" android:paddingLeft="10dp"
            android:paddingRight="10dp">
            <!--  tags -->
            <LinearLayout android:id="@+id/tagsLayout"
                android:orientation="horizontal" android:layout_width="fill_parent"
                android:layout_weight="1" android:layout_height="wrap_content" />
            <CheckBox android:id="@+id/save_button" style="@style/StarButton"
                android:layout_marginRight="10sp" />
        </LinearLayout>


        <!--  meta -->
        <Button android:id="@+id/meta" style="@style/Meta"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"/>


        <!-- pictures -->
        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:gravity="center" android:id="@+id/gallery_container">
            <Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>


        <!--  padded layout -->
        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="vertical"
            android:padding="5dp">

            <!-- summary -->
            <TextView android:id="@+id/summary" style="@style/AdDetailSummary"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:paddingBottom="10dp" />


        </LinearLayout>


        <!--  tab -->
        <LinearLayout android:layout_width="fill_parent"
            android:orientation="vertical" android:layout_height="wrap_content"
            android:background="@drawable/bottom_green_bar" android:paddingLeft="10dp">
            <TextView android:layout_width="wrap_content" android:id="@+id/tab_title"
                android:gravity="center" android:layout_height="wrap_content"
                android:text="@string/member_profile" style="@style/TabLabel" />
        </LinearLayout>

        <!-- user detail -->
        <LinearLayout android:layout_width="fill_parent"
            android:orientation="vertical" android:layout_height="wrap_content"
            android:background="@color/very_light_gray" android:paddingBottom="10dp">
            <!--  user item -->

            <include layout="@layout/user_item" />
            <include layout="@layout/website_item" />

        </LinearLayout>
    </LinearLayout>
</ScrollView>
</merge>

style.xml :

<style name="StarButton">
    <item name="android:background">@drawable/bk_star_button</item>
    <item name="android:layout_width">30dp</item>
    <item name="android:layout_height">30dp</item>
    <item name="android:button">@android:id/empty</item>
</style>

bk_star_button.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_star_big_off"
      android:state_checked="false"/>
    <item android:drawable="@drawable/btn_star_big_on"
      android:state_checked="true"/>          
</selector>

这是另一个布局,与某些设备上引发的完全相同的异常:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="@drawable/bk_gradient">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="left|center_vertical" style="@style/TopBar"
        android:id="@+id/titlebar">
        <ImageView android:src="@drawable/logo" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="@null"
        android:layout_alignParentLeft="true" />

        <FrameLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_centerVertical="true"
        android:paddingLeft="8dp" android:id="@+id/search_progressbar"
        android:visibility="gone">
            <ProgressBar android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            style="@style/ProgressBarMedium" />
        </FrameLayout>

    <LinearLayout android:id="@+id/buttons_container"
    android:gravity="right|center_vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>
    ....
</LinearLayout>
</LinearLayout>

引发异常的代码:

public static CompoundButton addTitleBarCompoundButton(Context context,
        View titleBar, CharSequence label, boolean isChecked) {
    if (!(titleBar instanceof ViewGroup))
        return null;

    ViewGroup group = (ViewGroup) titleBar
            .findViewById(R.id.buttons_container);

    // We attach to root during the inflating process othersize layout_
    // parameters in the style are ignored
    LayoutInflater.from(context).inflate(R.layout.topbar_toggle_button,
            group, true);

    // Get the result, which is the last child of the group
    CompoundButton result = (CompoundButton) group.getChildAt(group
            .getChildCount() - 1);
    result.setChecked(isChecked);

    // Set the text
    result.setText(label);
    return result;
}

topbar_toggle_button.xml :

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android" style="@style/BarButton.Toggle"/>

I am having weird stack trace from my application in the android market.
I have a view switcher which contains two views, one of them contains a checkbox.

I am getting this stack trace AdDetailView beeing a custom view.

  android.view.InflateException: Binary XML file line #29: Error inflating class android.widget.CheckBox
  at android.view.LayoutInflater.createView(LayoutInflater.java:513)
  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:382)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
  at com.yakaz.view.AdDetailView.init(AdDetailView.java:67)
  at com.yakaz.view.AdDetailView.<init>(AdDetailView.java:78)

This happens in the following view contructor :

private void init(Context context) {
    LayoutInflater li = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    li.inflate(R.layout.ad_detail_view, this);
    setOrientation(LinearLayout.VERTICAL);
}

public AdDetailView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

The line 67 is the inflate line.

The problem is that i can't reproduce the problem, on emulator or device.
Any pointers ?

Here is the ad_detail_view.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/preview_warning"
    style="@style/Notification" android:text="@string/this_is_only_a_preview"
    android:visibility="gone" />

<ScrollView android:layout_width="fill_parent"
    android:fadingEdge="none" android:layout_height="fill_parent"
    android:layout_weight="1" android:fillViewport="true" android:id="@+id/scrolls">
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:orientation="vertical">

        <include layout="@layout/action_bar" />

        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:paddingTop="10dp" android:paddingLeft="10dp"
            android:paddingRight="10dp">
            <!--  tags -->
            <LinearLayout android:id="@+id/tagsLayout"
                android:orientation="horizontal" android:layout_width="fill_parent"
                android:layout_weight="1" android:layout_height="wrap_content" />
            <CheckBox android:id="@+id/save_button" style="@style/StarButton"
                android:layout_marginRight="10sp" />
        </LinearLayout>


        <!--  meta -->
        <Button android:id="@+id/meta" style="@style/Meta"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"/>


        <!-- pictures -->
        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:gravity="center" android:id="@+id/gallery_container">
            <Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>


        <!--  padded layout -->
        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="vertical"
            android:padding="5dp">

            <!-- summary -->
            <TextView android:id="@+id/summary" style="@style/AdDetailSummary"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:paddingBottom="10dp" />


        </LinearLayout>


        <!--  tab -->
        <LinearLayout android:layout_width="fill_parent"
            android:orientation="vertical" android:layout_height="wrap_content"
            android:background="@drawable/bottom_green_bar" android:paddingLeft="10dp">
            <TextView android:layout_width="wrap_content" android:id="@+id/tab_title"
                android:gravity="center" android:layout_height="wrap_content"
                android:text="@string/member_profile" style="@style/TabLabel" />
        </LinearLayout>

        <!-- user detail -->
        <LinearLayout android:layout_width="fill_parent"
            android:orientation="vertical" android:layout_height="wrap_content"
            android:background="@color/very_light_gray" android:paddingBottom="10dp">
            <!--  user item -->

            <include layout="@layout/user_item" />
            <include layout="@layout/website_item" />

        </LinearLayout>
    </LinearLayout>
</ScrollView>
</merge>

style.xml :

<style name="StarButton">
    <item name="android:background">@drawable/bk_star_button</item>
    <item name="android:layout_width">30dp</item>
    <item name="android:layout_height">30dp</item>
    <item name="android:button">@android:id/empty</item>
</style>

bk_star_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_star_big_off"
      android:state_checked="false"/>
    <item android:drawable="@drawable/btn_star_big_on"
      android:state_checked="true"/>          
</selector>

here is another layout that as the exact same exception raised on some device :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="@drawable/bk_gradient">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="left|center_vertical" style="@style/TopBar"
        android:id="@+id/titlebar">
        <ImageView android:src="@drawable/logo" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="@null"
        android:layout_alignParentLeft="true" />

        <FrameLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_centerVertical="true"
        android:paddingLeft="8dp" android:id="@+id/search_progressbar"
        android:visibility="gone">
            <ProgressBar android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            style="@style/ProgressBarMedium" />
        </FrameLayout>

    <LinearLayout android:id="@+id/buttons_container"
    android:gravity="right|center_vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>
    ....
</LinearLayout>
</LinearLayout>

the code that raises the exception :

public static CompoundButton addTitleBarCompoundButton(Context context,
        View titleBar, CharSequence label, boolean isChecked) {
    if (!(titleBar instanceof ViewGroup))
        return null;

    ViewGroup group = (ViewGroup) titleBar
            .findViewById(R.id.buttons_container);

    // We attach to root during the inflating process othersize layout_
    // parameters in the style are ignored
    LayoutInflater.from(context).inflate(R.layout.topbar_toggle_button,
            group, true);

    // Get the result, which is the last child of the group
    CompoundButton result = (CompoundButton) group.getChildAt(group
            .getChildCount() - 1);
    result.setChecked(isChecked);

    // Set the text
    result.setText(label);
    return result;
}

topbar_toggle_button.xml :

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android" style="@style/BarButton.Toggle"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清泪尽 2024-11-15 04:05:02

我怀疑这与您将 android:button 参数设置为 @android:id/empty 有关。您能详细说明一下您这样做的原因吗?在我看来,您已经通过背景上的 创建了自定义选中/未选中视图,因此应该清除按钮元素。如果我的假设是正确的,您应该以这种方式执行此操作:

<style name="StarButton">
  <item name="android:background">@drawable/bk_star_button</item>
  <item name="android:layout_width">30dp</item>
  <item name="android:layout_height">30dp</item>
  <item name="android:button">@null</item>
</style>

提供 @null 属性作为清除默认属性的方式,而 @android:id/empty 是系统用来引用 AdapterView 实例在没有可显示的项目时呈现的“空”视图的 ID。这种差异可能会导致许多未知的行为。

I suspect it has something to do with you setting the android:button parameter to @android:id/empty. Can you elaborate on why you are doing this? It seems to me that you have created custom checked/unchecked views via a <selector> on the background, so the button element should be cleared out. If my assumption is correct, you should do that in this fashion:

<style name="StarButton">
  <item name="android:background">@drawable/bk_star_button</item>
  <item name="android:layout_width">30dp</item>
  <item name="android:layout_height">30dp</item>
  <item name="android:button">@null</item>
</style>

The @null attribute is provided as a way to clear default attributes, whereas @android:id/empty is the id used by the system to reference the "empty" view that is presented by AdapterView instances when they have no items to display. This discrepancy could cause a host of unknown behaviors.

芯好空 2024-11-15 04:05:02

有同样的问题,尝试使用 android.support.v7.widget.AppCompatCheckBox

<android.support.v7.widget.AppCompatCheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/crime_solved"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:text="@string/crime_solved_label"/>

Had same problem, try using android.support.v7.widget.AppCompatCheckBox

<android.support.v7.widget.AppCompatCheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/crime_solved"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:text="@string/crime_solved_label"/>
悲念泪 2024-11-15 04:05:02

如果您从 viewroot 的构造函数调用 inflate,那么您应该使用 attachToRoot 参数 false 调用它:

li.inflate(R.layout.ad_detail_view, this);

因为它尚未实例化(您当前位于其构造函数中)。

但根据您收到的错误,这可能不是唯一的问题,因此共享 ad_detail_view 的 xml 布局可能会有所帮助(错误表明问题出在 xml 文件中,第 29 行) 。

If you are calling inflate from the constructor of the viewroot, then you should call it with attachToRoot parameter false:

li.inflate(R.layout.ad_detail_view, this);

since it is not instantiated yet (you are currently in its constructor).

But based on the error you get, this might not be the only problem, so sharing the xml layout of your ad_detail_view might help (the error says the problem is in the xml file, on line #29).

夏九 2024-11-15 04:05:02

(我的建议)可能是,当您定义样式时,您不会将默认复选框样式指定为父级。因此,在某些 Android/设备中,您缺少在该父级中定义的属性。

或者(因为此异常会导致 CheckBox 本身出现问题)尝试将其移动到您的 apk (widget.CheckBox.java)。

Android Market 不允许您检查导致崩溃的 Android 版本,这是一件坏事。您是否尝试过使用 Android 1.5 运行模拟器来检查这一点?

It might be (my suggestion) that when you're defining style, you're not giving as parent default CheckBox style. Therefore, in some Android/Device you're missing property that defined in that parent.

Alternatively (as this exception causes problems with CheckBox itself) try to move it to your apk (widget.CheckBox.java).

That's a bad thing that Android Market doesn't allow you to check android version that causes crash. Have you tried to run emulator with Android 1.5 to check this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文