在平板电脑上膨胀 GridView 时出错

发布于 2025-01-04 12:19:08 字数 3534 浏览 0 评论 0原文

我在 Android 应用程序中使用 GridView。一切都在我的手机上完美运行,但是当我尝试在平板电脑模拟器上运行该应用程序时,它一开始就崩溃了。

错误是 E/AndroidRuntime(466): java.lang.RuntimeException: 无法启动 Activity ComponentInfo{****/***.Main}: java.lang.RuntimeException: 无法启动 Activity ComponentInfo{* **/***.views.calendar.CalendarView}:android.view.InflateException:二进制XML文件行#42:错误膨胀类android.widget.GridView 第 #42 行仅包含一个 标记。

然后堆栈跟踪显示 Caused by: android.view.InflateException: Binary XML file line #42: Error inflating class android.widget.GridView, Caused by: java.lang.reflect.InspirationTargetException< /code> 并结束:

02-11 23:57:17.079: E/AndroidRuntime(466): Caused by: android.content.res.Resources$NotFoundException: File  from drawable resource ID #0x1020004
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.content.res.Resources.loadDrawable(Resources.java:1874)
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.widget.AbsListView.<init>(AbsListView.java:711)
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.widget.GridView.<init>(GridView.java:76)
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.widget.GridView.<init>(GridView.java:72)

我不使用任何 LayoutParams,我的 xml 布局如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/calendar_relativeLayout_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/calendar_relativeLayout_infoBar"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@color/calendar_infoBar_background" >

        <ImageButton
            android:id="@+id/calendar_imageButton_previousMonth"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:background="@null"
            android:src="@drawable/ic_calendar_prev" />

        <ImageButton
            android:id="@+id/calendar_imageButton_nextMonth"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:background="@null"
            android:src="@drawable/ic_calendar_next" />

        <TextView
            android:id="@+id/calendar_textView_currentMonth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="February 2012"
            android:textSize="17dp" />

    </RelativeLayout>

    <GridView
        android:id="@+id/calendar_gridView_currentCalendar"
        android:layout_width="fill_parent"
        android:layout_height="240dp"
        android:layout_below="@+id/calendar_relativeLayout_infoBar"
        android:numColumns="7"
        android:isScrollContainer="false"
        android:listSelector="@android:id/empty">
    </GridView>

</RelativeLayout>

我不明白的是 GridView 是如何工作的在手机上无法在平板电脑上使用...

预先感谢您提供任何线索...

I'm using a GridView in my Android app. Everything works perfectly on my phone, but when I tried to run the app on a tablet emulator, it crashed at the start.

The error is E/AndroidRuntime(466): java.lang.RuntimeException: Unable to start activity ComponentInfo{****/***.Main}: java.lang.RuntimeException: Unable to start activity ComponentInfo{***/***.views.calendar.CalendarView}: android.view.InflateException: Binary XML file line #42: Error inflating class android.widget.GridView
The line #42 has simply a <GridView> tag in it.

Then the stacktrace says Caused by: android.view.InflateException: Binary XML file line #42: Error inflating class android.widget.GridView, Caused by: java.lang.reflect.InvocationTargetException and finishes with:

02-11 23:57:17.079: E/AndroidRuntime(466): Caused by: android.content.res.Resources$NotFoundException: File  from drawable resource ID #0x1020004
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.content.res.Resources.loadDrawable(Resources.java:1874)
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.widget.AbsListView.<init>(AbsListView.java:711)
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.widget.GridView.<init>(GridView.java:76)
02-11 23:57:17.079: E/AndroidRuntime(466):  at android.widget.GridView.<init>(GridView.java:72)

I don't use any LayoutParams, my xml layout looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/calendar_relativeLayout_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/calendar_relativeLayout_infoBar"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@color/calendar_infoBar_background" >

        <ImageButton
            android:id="@+id/calendar_imageButton_previousMonth"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:background="@null"
            android:src="@drawable/ic_calendar_prev" />

        <ImageButton
            android:id="@+id/calendar_imageButton_nextMonth"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:background="@null"
            android:src="@drawable/ic_calendar_next" />

        <TextView
            android:id="@+id/calendar_textView_currentMonth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="February 2012"
            android:textSize="17dp" />

    </RelativeLayout>

    <GridView
        android:id="@+id/calendar_gridView_currentCalendar"
        android:layout_width="fill_parent"
        android:layout_height="240dp"
        android:layout_below="@+id/calendar_relativeLayout_infoBar"
        android:numColumns="7"
        android:isScrollContainer="false"
        android:listSelector="@android:id/empty">
    </GridView>

</RelativeLayout>

What I don't understand is that how come a GridView is workable on a phone and not workable in a tablet...

Thanks in advance for any clues...

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

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

发布评论

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

评论(1

毁虫ゝ 2025-01-11 12:19:08
android:listSelector="@android:id/empty"

据我所知 listSelector 属性 需要一个引用到一个drawable,而不是一个id。 @android:id/empty 可用于为 ListView 和 GridView 声明可选的“空”视图,我很确定它不应该像上面那样使用。有关更多详细信息,请参阅 ListActivity。尝试引用实际的可绘制对象(或颜色),例如 @null@android:color/transparent 以使选择器“不可见”(或者只是将高度设置为“0” ')。

或者,您可以使用资源限定符来阻止平板电脑找到适当的资源。阅读“Android 如何查找最佳匹配资源” 一些更多细节的解释。但是,由于您没有在 GridView 声明中直接引用可绘制资源,因此我怀疑这就是问题所在。

android:listSelector="@android:id/empty"

As far as I know the listSelector attribute expects a reference to a drawable, not an id. @android:id/empty can be used to declare an optional 'empty' view for ListView and GridView, and I'm pretty sure it's not meant to used as above. See ListActivity for more details. Try referencing an actual drawable (or color), e.g. @null or @android:color/transparent to make the selector 'invisible' (or just set the height to '0').

Alternatively, you could be using resource qualifiers that prevent a tablet from finding the appropriate resource. Have a read through the 'How Android Finds the Best-matching Resource' explanation for some more details. However, since you're not referencing a drawable resource directly in your GridView declaration, I doubt this is the issue.

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