Android:如何追踪 InflateException 的来源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2605999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to track down the origin of a InflateException?
提问by Janusz
While starting my application I get the following warning in Logcat:
在启动我的应用程序时,我在 Logcat 中收到以下警告:
04-09 10:28:17.830: WARN/WindowManager(52): Exception when adding starting window
04-09 10:28:17.830: WARN/WindowManager(52): android.view.InflateException: Binary XML file line #24: Error inflating class <unknown>
04-09 10:28:17.830: WARN/WindowManager(52): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
04-09 10:28:17.830: WARN/WindowManager(52): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-09 10:28:17.830: WARN/WindowManager(52): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
04-09 10:28:17.830: WARN/WindowManager(52): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
04-09 10:28:17.830: WARN/WindowManager(52): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-09 10:28:17.830: WARN/WindowManager(52): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-09 10:28:17.830: WARN/WindowManager(52): at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2153)
04-09 10:28:17.830: WARN/WindowManager(52): at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2207)
04-09 10:28:17.830: WARN/WindowManager(52): at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1395)
04-09 10:28:17.830: WARN/WindowManager(52): at com.android.internal.policy.impl.PhoneWindowManager.addStartingWindow(PhoneWindowManager.java:818)
04-09 10:28:17.830: WARN/WindowManager(52): at com.android.server.WindowManagerService$H.handleMessage(WindowManagerService.java:8794)
04-09 10:28:17.830: WARN/WindowManager(52): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 10:28:17.830: WARN/WindowManager(52): at android.os.Looper.loop(Looper.java:123)
04-09 10:28:17.830: WARN/WindowManager(52): at com.android.server.WindowManagerService$WMThread.run(WindowManagerService.java:531)
04-09 10:28:17.830: WARN/WindowManager(52): Caused by: java.lang.reflect.InvocationTargetException
04-09 10:28:17.830: WARN/WindowManager(52): at android.widget.FrameLayout.<init>(FrameLayout.java:79)
04-09 10:28:17.830: WARN/WindowManager(52): at java.lang.reflect.Constructor.constructNative(Native Method)
04-09 10:28:17.830: WARN/WindowManager(52): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
04-09 10:28:17.830: WARN/WindowManager(52): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
04-09 10:28:17.830: WARN/WindowManager(52): ... 13 more
04-09 10:28:17.830: WARN/WindowManager(52): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x1010059 a=-1}
04-09 10:28:17.830: WARN/WindowManager(52): at android.content.res.Resources.loadDrawable(Resources.java:1677)
04-09 10:28:17.830: WARN/WindowManager(52): at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
04-09 10:28:17.830: WARN/WindowManager(52): at android.widget.FrameLayout.<init>(FrameLayout.java:91)
04-09 10:28:17.830: WARN/WindowManager(52): ... 17 more
My Application starts with the following splash screen:
我的应用程序从以下闪屏开始:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:windowBackground="@color/white" android:background="@color/white"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:foregroundGravity="center">
<ImageView android:id="@+id/ImageView01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:adjustViewBounds="true" android:scaleType="centerInside"
android:src="@drawable/splash" android:layout_gravity="center" />
</ScrollView>
Splash is the image that is shown in the splash screen. I have those four folders with for storing drawables in my app:
启动画面是显示在启动画面中的图像。我有这四个文件夹用于在我的应用程序中存储可绘制对象:
/res/drawable-hdpi
/res/drawable-ldpi
/res/drawable-mdpi
/res/drawable-nodpi
/res/drawable-hdpi
/res/drawable-ldpi
/res/drawable-mdpi
/res/drawable-nodpi
the splash image has its own version in the first three of them and is displayed properly. Removing the src property from the ImageView removes the image but not the exception.
初始图像在前三个中有自己的版本,并且显示正确。从 ImageView 中删除 src 属性会删除图像,但不会删除异常。
I'm a little bit lost with where to look for the cause of the exception. I even don't know if this is really an issue in this layout file etc.
我有点迷失在哪里寻找异常的原因。我什至不知道这是否真的是这个布局文件等中的问题。
How would you go about finding the cause for this warning?
您将如何查找此警告的原因?
回答by Garrett
I had the same exact error. The only xml line 24 that made any sense was my application manifest which happened to be the closing tag for application. I traced it down to my custom theme I was adding for the app -- I was not defining the parent of the style. All I needed to do was add parent="android:Theme" to my style and my error went away.
我有同样的错误。唯一有意义的 xml 第 24 行是我的应用程序清单,它恰好是应用程序的结束标记。我将其追溯到我为应用程序添加的自定义主题——我没有定义样式的父级。我需要做的就是将 parent="android:Theme" 添加到我的样式中,然后我的错误就消失了。
<style name="MyTheme" parent="android:Theme">
...
</style>
回答by Eng.Fouad
I had the same issue, and it was because I passed the application context instead of the activity context.
我遇到了同样的问题,这是因为我传递了应用程序上下文而不是活动上下文。
回答by Jim Blackler
04-09 10:28:17.830: WARN/WindowManager(52): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x1010059 a=-1
That line is the giveaway. The system isn't happy with one of your resources, probably a 'src' resource, probably android:src="@drawable/splash"
.
那条线是赠品。系统对您的资源之一不满意,可能是“src”资源,可能是android:src="@drawable/splash"
.
回答by Nada
I got a similar error when running on a 2.0 emulator. I used a newer version of the API (version 7, 2.1-update1) and it worked.
我在 2.0 模拟器上运行时遇到了类似的错误。我使用了较新版本的 API(版本 7,2.1-update1)并且它有效。
回答by Steve Haley
The error is on line 24 of your XML file. What does that line contain? By the looks of the error message
错误位于 XML 文件的第 24 行。该行包含什么?从错误消息的外观来看
android.view.InflateException: Binary XML file line #24: Error inflating class <unknown>
you're using a custom class and haven't defined something properly. For example, did you define the package name properly? Maybe its constructor isn't formatted? We'd have to see the XML file, specifically what's around line 24, to be able to give more info.
您正在使用自定义类并且没有正确定义某些内容。例如,您是否正确定义了包名称?也许它的构造函数没有格式化?我们必须查看 XML 文件,特别是第 24 行附近的内容,才能提供更多信息。
回答by Gravitoid
I know this is a bit late but I spent all day trying to figure this one out and finally with the help of thisSO question I realized an XML file in the example I was trying out was using a feature from a later API and Lint wasn't catching it because the file hadn't changed. As soon as I did a random edit on the XML file and saved the file then the error about a later API showed up.
我知道这有点晚了,但我花了一整天的时间试图解决这个问题,最后在这个SO question的帮助下,我意识到我正在尝试的示例中的一个 XML 文件使用了来自后来的 API 的功能,而 Lint 不是没有抓住它,因为文件没有改变。一旦我对 XML 文件进行了随机编辑并保存了文件,就会出现有关稍后 API 的错误。
回答by Ahmad Arslan
After so many hectic research on finding the answer that why App is crashing and why its not showing the logcat
经过这么多忙碌的研究,找到了为什么 App 崩溃以及为什么它不显示 logcat 的答案
I have just replaced this
我刚换了这个
android:configChanges="orientation|keyboardHidden"
to
到
android:configChanges="keyboardHidden|orientation|screenSize"
in the activity tag.
在活动标签中。