gpt4 book ai didi

android - 使用 DrawerLayout 时发生未知错误?

转载 作者:行者123 更新时间:2023-11-29 15:22:20 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

唯一的问题是您用来扩充列表项的 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));

它应该可以工作。

美国东部时间:如果您对 android 2.2 或 2.3 等目标使用相同的代码,您应该从您的

drawer_list_item.xml

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

关于android - 使用 DrawerLayout 时发生未知错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17135106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com