Custom listview entry works in JB not in Gingerbread

Posted by Andy on Stack Overflow See other posts from Stack Overflow or by Andy
Published on 2012-10-04T21:35:02Z Indexed on 2012/10/04 21:37 UTC
Read the original article Hit count: 593

Filed under:
|

I have a ListFragment with a custom ArrayAdapter where I am overiding getView() to provide a custom View for the list item.

private class DirListAdaptor extends ArrayAdapter<DirItem> {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View aView = convertView;
        if (aView == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // TODO: can we not access textViewResourceId?
            aView = vi.inflate(R.layout.dir_list_entry, parent, false);
        }
 etc...

Here is the dir_list_entry.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
            android:paddingRight="?android:attr/listPreferredItemPaddingRight">

<ImageView
    android:id="@+id/dir_list_icon"

    android:layout_width="wrap_content"
    android:layout_height="match_parent"

    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="6dp"
    android:src="@drawable/ic_launcher"
    />

<TextView
    android:id="@+id/dir_list_details"

    android:textAppearance="?android:attr/textAppearanceListItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_toRightOf="@id/dir_list_icon"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"

    android:singleLine="true"
    android:ellipsize="marquee"

    android:textSize="12sp"
    android:text="Details" />

<TextView
    android:id="@+id/dir_list_filename"

    android:textAppearance="?android:attr/textAppearanceListItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_toRightOf="@id/dir_list_icon"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_above="@id/dir_list_details"
    android:layout_alignWithParentIfMissing="true"

    android:gravity="center_vertical"
    android:textSize="14sp"

    android:text="Filename"/>

</RelativeLayout>

The bizarre thing is this works fine on Android 4.1 emulator, but I get the following error on Android 2.3:

10-01 15:07:59.594: ERROR/AndroidRuntime(1003): FATAL EXCEPTION: main
    android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at com.eveps.evepsdroid.ui.PhotoBrowserListFragment$DirListAdaptor.getView(PhotoBrowserListFragment.java:104)
    at android.widget.AbsListView.obtainView(AbsListView.java:1430)
    at android.widget.ListView.makeAndAddView(ListView.java:1745)
    at android.widget.ListView.fillDown(ListView.java:670)
    at android.widget.ListView.fillFromTop(ListView.java:727)
    at android.widget.ListView.layoutChildren(ListView.java:1598)
    at android.widget.AbsListView.onLayout(AbsListView.java:1260)
    at android.view.View.layout(View.java:7175)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    at android.view.View.layout(View.java:7175)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    at android.view.View.layout(View.java:7175)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    at android.view.View.layout(View.java:7175)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    at android.view.View.layout(View.java:7175)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
    at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1243)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1049)
    at android.view.View.layout(View.java:7175)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    at android.view.View.layout(View.java:7175)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
    at android.view.View.layout(View.java:7175)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    at android.view.View.layout(View.java:7175)
    at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
    ... 42 more
    Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
    at android.view.View.<init>(View.java:1957)
    at android.view.View.<init>(View.java:1899)
    at android.view.ViewGroup.<init>(ViewGroup.java:286)
    at android.widget.RelativeLayout.<init>(RelativeLayout.java:173)
    ... 45 more

I'm using the Android Support library for fragment support obviously.

Seems to be a problem inflating the custom list view entry, something to do with a dimension - but why does it work on JellyBean? Has something changed in this area?

© Stack Overflow or respective owner

Related posts about android

Related posts about android-layout