自定义列表视图条目在JB中不适用于Gingerbread

时间:2012-10-04 21:35:02

标签: android android-layout

我有一个带有自定义ArrayAdapter的ListFragment,我在其中覆盖getView()以提供列表项的自定义视图。

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...

这是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>

奇怪的是这在Android 4.1模拟器上工作正常,但我在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

我正在使用Android支持库来显示片段支持。

似乎是一个让自定义列表视图条目膨胀的问题,与维度有关 - 但为什么它可以在JellyBean上运行?这方面有变化吗?

2 个答案:

答案 0 :(得分:1)

textAppearanceListItem不是Gingerbread SDK的一部分。

答案 1 :(得分:1)

您正在使用Gingerbread中不存在的属性。以下不存在:

  • listPreferredItemPaddingLeft
  • listPreferredItemPaddingRight
  • textAppearanceListItem

您可以通过查看/platforms/android-version/data/res/values/attrs.xml下的SDK文件夹来仔细检查属性是否存在。