我正在尝试使用official教程从支持库中使用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"/>
如果需要更多信息,请发表评论,我会更新问题。
答案 0 :(得分: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这样的目标使用相同的代码,你应该从你的
中删除这些行drawer_list_item.xml
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:background="?android:attr/activatedBackgroundIndicator"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
答案 1 :(得分:2)
这似乎来自您ListView
内的DrawerLayout
,而不是来自DrawerLayout
本身。在行布局的某处,您指的是@color/primary_text_dark
,并且项目中不存在该颜色。
答案 2 :(得分:0)
有相同的症状,通过更改数组适配器构造函数来解决:
new ArrayAdapter<String>(getActivity().getContext(),
R.layout.drawer_list_item, mDrawerTitles));
为:
new ArrayAdapter<String>(getActivity(),
R.layout.drawer_list_item, mDrawerTitles);
不完全确定原因,但是通过在构造函数中将Activity作为Context传递,一切都恢复正常。