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…
As far as I know the listSelector attribute expects a reference to a drawable, not an id.
@android:id/emptycan 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. SeeListActivityfor more details. Try referencing an actual drawable (or color), e.g.@nullor@android:color/transparentto 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.