Unknown error while using DrawerLayout?

元气小坏坏 提交于 2019-12-24 06:58:53

问题


I am trying to use DrawerLayout from Support library using official tutorial but whenever I tried to run my application I got crash .After reading the below logs I am not able to identify the location in my code which is responsible for this crash.

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

My Layout file:

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

My 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"/>

If More information is required then please comment and I will update the question.


回答1:


I happened to face the same problem yesterday. And here's what I did to fix that.

Only problem is of the TextView that you are using to inflate your list Item. So if you are using something like

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

Just replace it with

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

and it should work.

EDT: If you are using the same code for a target like android 2.2 or 2.3, you should remove these lines from your

drawer_list_item.xml

android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:background="?android:attr/activatedBackgroundIndicator"
android:textAppearance="?android:attr/textAppearanceListItemSmall"



回答2:


This appears to be coming from your ListView that you have inside of the DrawerLayout, not from the DrawerLayout itself. Somewhere in your row layout, you are referring to @color/primary_text_dark, and that color does not exist in your project.




回答3:


Had the same symptoms, fixed by changing the array adapter constructor from:

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

to:

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

Not exactly sure why but by passing the Activity as a Context in the constructor, everything went back to normal.



来源:https://stackoverflow.com/questions/17135106/unknown-error-while-using-drawerlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!