1

我正在尝试使用官方教程从支持库中使用 DrawerLayout,但是每当我尝试运行我的应用程序时,我都会崩溃。阅读以下日志后,我无法确定我的代码中导致此崩溃的位置。

06-16 21:14:55.272: E/AndroidRuntime(20104): FATAL EXCEPTION: main
06-16 21:14:55.272: E/AndroidRuntime(20104): android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.LayoutInflater.createView(LayoutInflater.java:513)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.AbsListView.obtainView(AbsListView.java:1340)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.ListView.makeAndAddView(ListView.java:1789)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.ListView.fillDown(ListView.java:656)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.ListView.fillFromTop(ListView.java:722)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.ListView.layoutChildren(ListView.java:1624)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.AbsListView.onLayout(AbsListView.java:1172)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.View.layout(View.java:7080)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:672)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.View.layout(View.java:7080)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.View.layout(View.java:7080)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.View.layout(View.java:7080)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1056)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1738)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.os.Looper.loop(Looper.java:143)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.app.ActivityThread.main(ActivityThread.java:4717)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at java.lang.reflect.Method.invokeNative(Native Method)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at java.lang.reflect.Method.invoke(Method.java:521)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at dalvik.system.NativeStart.main(Native Method)
06-16 21:14:55.272: E/AndroidRuntime(20104): Caused by: java.lang.reflect.InvocationTargetException
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.TextView.<init>(TextView.java:426)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at java.lang.reflect.Constructor.constructNative(Native Method)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.LayoutInflater.createView(LayoutInflater.java:500)
06-16 21:14:55.272: E/AndroidRuntime(20104):    ... 29 more
06-16 21:14:55.272: E/AndroidRuntime(20104): Caused by: android.content.res.Resources$NotFoundException: File res/color/primary_text_dark.xml from drawable resource ID #0x0
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.content.res.Resources.loadDrawable(Resources.java:1725)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.view.View.<init>(View.java:1902)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.widget.TextView.<init>(TextView.java:432)
06-16 21:14:55.272: E/AndroidRuntime(20104):    ... 33 more
06-16 21:14:55.272: E/AndroidRuntime(20104): Caused by: android.content.res.Resources$NotFoundException: File res/color/primary_text_dark.xml from xml type drawable resource ID #0x0
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:1952)
06-16 21:14:55.272: E/AndroidRuntime(20104):    at android.content.res.Resources.loadDrawable(Resources.java:1720)
06-16 21:14:55.272: E/AndroidRuntime(20104):    ... 36 more

我的布局文件:

<?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" >

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->

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

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

我的drawer_list_item.xml

<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:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

如果需要更多信息,请发表评论,我会更新问题。

4

3 回答 3

3

我昨天碰巧遇到了同样的问题。这就是我为解决这个问题所做的。

唯一的问题是TextView你用来膨胀你的列表项。所以如果你使用类似的东西

mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, listarray));

只需将其替换为

mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listarray));

它应该可以工作。

EDT:如果您对像 android 2.2 或 2.3 这样的目标使用相同的代码,您应该从您的

抽屉列表项目.xml

android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:background="?android:attr/activatedBackgroundIndicator"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
于 2013-10-10T07:11:44.613 回答
2

这似乎来自你ListView在 内部的DrawerLayout,而不是来自DrawerLayout自身。在您的行布局中的某处,您指的是@color/primary_text_dark,并且该颜色在您的项目中不存在。

于 2013-06-16T16:24:13.607 回答
0

有相同的症状,通过更改数组适配器构造函数来修复:

new ArrayAdapter<String>(getActivity().getContext(),
            R.layout.drawer_list_item, mDrawerTitles));

至:

new ArrayAdapter<String>(getActivity(),
            R.layout.drawer_list_item, mDrawerTitles);

不完全确定为什么,但通过在构造函数中将 Activity 作为 Context 传递,一切恢复正常。

于 2014-02-06T12:37:15.163 回答