4

我已经实现了基于 ListView 的导航抽屉。它适用于 ICS 及以上版本的 Android。但是,在旧版本上,它会因以下错误而崩溃:

06-23 15:50:11.570: E/AndroidRuntime(403): Caused by: 
android.content.res.Resources$NotFoundException: 
File res/drawable/list_selector_background.xml 
from xml type drawable resource ID #0x0

我曾尝试将这个特定的 xml 文件从 Android sdk 复制到我自己的项目中,但这并没有帮助。

这是xml文件(删节):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#dddddd"
        android:choiceMode="singleChoice"
        android:divider="@color/gray"
        android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

和代码:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.openDrawer(mDrawerList);

完整的堆栈跟踪:

Uncaught handler: thread main exiting due to uncaught exception
android.view.InflateException: 
    Binary XML file line #1: Error inflating class <unknown>

    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.inflate(LayoutInflater.java:385)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
    at android.widget.AbsListView.obtainView(AbsListView.java:1274)
    at android.widget.ListView.makeAndAddView(ListView.java:1668)
    at android.widget.ListView.fillDown(ListView.java:637)
    at android.widget.ListView.fillFromTop(ListView.java:694)
    at android.widget.ListView.layoutChildren(ListView.java:1521)
    at android.widget.AbsListView.onLayout(AbsListView.java:1113)
    at android.view.View.layout(View.java:6830)
    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:672)
    at android.view.View.layout(View.java:6830)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    at android.view.View.layout(View.java:6830)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
    at android.view.View.layout(View.java:6830)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    at android.view.View.layout(View.java:6830)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    at android.view.View.layout(View.java:6830)
    at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4363)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.reflect.InvocationTargetException
    at android.widget.TextView.<init>(TextView.java:320)
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
    at android.view.LayoutInflater.createView(LayoutInflater.java:500)
    ... 35 more

Caused by: android.content.res.Resources$NotFoundException: 
    File res/drawable/list_selector_background.xml from drawable resource ID #0x0
    at android.content.res.Resources.loadDrawable(Resources.java:1693)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
    at android.view.View.<init>(View.java:1850)
    at android.widget.TextView.<init>(TextView.java:326)
    ... 39 more

Caused by: android.content.res.Resources$NotFoundException: 
    File res/drawable/list_selector_background.xml 
    from xml type drawable resource ID #0x0
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:1920)
    at android.content.res.Resources.loadDrawable(Resources.java:1688)
    ... 42 more
4

2 回答 2

8

导航抽屉的官方 Google 示例,将此 TextView 作为抽屉列表项的一部分。

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

但是,使用这三个属性值,应用程序在 ICS 之前的 android 操作系统上会失败。我通过提供我自己的自定义值(与这些值不同的任何值)进行了验证,它在每个版本上都像魔术一样工作。

于 2013-06-25T15:25:17.417 回答
8

官方导航抽屉示例 的最低 API 级别为 14。如果此最小值设置为 ICS 版本(例如 11),这将导致强制关闭,因为方法 setHomeButton 仅在 API-14 中可用。

getActionBar().setHomeButtonEnabled(true);

当注释掉前面的语句时,这会导致 inflate 异常,这是使用 API-14 中添加的 Android 维度的结果。

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:textAppearance="?android:attr/textAppearanceListItemSmall"
      android:background="?android:attr/activatedBackgroundIndicator"
      android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

对于我支持 API-11+ 的应用程序,我已经解决了这个问题,如下所示。

1) 在drawer_list_item.xml 中引用自定义属性

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/myapp_activatedBackgroundIndicator"
    android:gravity="center_vertical"
    android:minHeight="?attr/myapp_listPreferredItemHeightSmall"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textAppearance="?attr/myapp_textAppearanceListItemSmall" />

2) 在 values/attrs.xml 中定义您的自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Theme">

    <!-- Attributes below are needed to support the navigation drawer on Android 3.x. -->
    <!-- A smaller, sleeker list item height. -->
    <attr name="myapp_listPreferredItemHeightSmall" format="dimension" />

    <!-- Drawable used as a background for activated items. -->
    <attr name="myapp_activatedBackgroundIndicator" format="reference" />

    <!-- The preferred TextAppearance for the primary text of small list items. -->
    <attr name="myapp_textAppearanceListItemSmall" format="reference" />
</declare-styleable>
</resources>

3) 在 values-11/styles.xml

<resources>
    <!-- Base application theme for API 11+. This theme completely replaces -->
    <!-- AppBaseTheme from res/values/styles.xml on API 11+ devices. -->
      <style name="AppBaseTheme" parent="android:Theme.Holo">

          <!-- API 11 theme customizations can go here. -->


          <!-- Implementation of attributes needed for the navigation drawer as the default implementation is based on API-14. -->
          <item name="myapp_listPreferredItemHeightSmall">48dip</item>
          <item name="myapp_textAppearanceListItemSmall">@style/MyappDrawerMenu</item>
          <item name="myapp_activatedBackgroundIndicator">@drawable/ab_transparent_action_bar</item>
      </style>

      <style name="MyappDrawerMenu">
          <item name="android:textSize">16sp</item>
          <item name="android:textStyle">bold</item>
          <item name="android:textColor">?android:attr/actionMenuTextColor</item>
      </style>

</resources>

4) 在 values-v14/styles.xml

   <resources>

       <!-- Base application theme for API 14+. This theme completely replaces -->
       <!-- AppBaseTheme from BOTH res/values/styles.xml and -->
       <!-- res/values-v11/styles.xml on API 14+ devices. -->

       <style name="AppBaseTheme" parent="android:Theme.Holo">

           <!-- For API-14 and above the default implementation of the navigation drawer menu is used. Below APU-14 a custom implementation is used. -->
           <item name="myapp_listPreferredItemHeightSmall">?android:attr/listPreferredItemHeightSmall</item>
           <item name="myapp_textAppearanceListItemSmall">?android:attr/textAppearanceListItemSmall</item>
           <item name="myapp_activatedBackgroundIndicator">?android:attr/activatedBackgroundIndicator</item>
       </style>

   </resources>

如果您想支持旧版本/其他版本,则需要为该特定版本添加 styles.xml。

于 2013-08-17T15:28:22.737 回答