错误膨胀Android Lollipop中的类android.support.v7.widget.Toolbar [英] Error inflating class android.support.v7.widget.Toolbar in Android Lollipop
问题描述
我将android.support库用于我的项目,我使用android 4.4开发了我的应用程序,并且一切正常,但是当我尝试在具有Android Lollipop的设备中运行该应用程序时出现了问题.
I use the android.support library for my project, I have developed my app using android 4.4 and everything works good but my problem comes when I try to run the application in a device with Android Lollipop.
使用工具栏加载活动时,应用程序停止并返回此错误.
When it loads the Activity with the Toolbar, the application stops and returns this error.
膨胀类android.support.v7.widget.Toolbar的错误
我在互联网上也发现了同样的问题,但是在答案中却没有找到解决方法.
I have found the same problem on the internet but I haven't found the solution in the answers.
这是我的styles.xml
This is my styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!-- Base theme applied no matter what API -->
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/dark_primary</item>
<item name="colorAccent">@color/accent_color</item>
</style>
这是我的styles.xml(v22)
Here is my styles.xml (v22)
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/dark_primary</item>
<item name="colorAccent">@color/accent_color</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
要在.xml中定义工具栏
To define the toolbar in .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mitoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
tools:showIn="@layout/activity_main" />
在MainActivty中,我只包含该工具栏:
And in my MainActivty I just include that toolbar:
<include layout="@layout/toolbar" />
最后是我的MainActivity.java的标头
And finally the header of my MainActivity.java
public class MainActivity extends android.support.v7.app.ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Here stops my application
}
这里是logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fitness.dullmonkey.keepingfit/com.fitness.dullmonkey.keepingfit.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5691)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.fitness.dullmonkey.keepingfit.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5691)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.fitness.dullmonkey.keepingfit.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5691)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 216
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
at android.view.View.<init>(View.java:4084)
at android.view.ViewGroup.<init>(ViewGroup.java:524)
at android.view.ViewGroup.<init>(ViewGroup.java:520)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:195)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:191)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.fitness.dullmonkey.keepingfit.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5691)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
预先感谢您的帮助.
推荐答案
错误的原因是,在工具栏xml中,?attr
前缀是指支持库主题中的属性.另一方面,在上述style.xml
的v22下,使用系统主题android:Theme.Material.Light.NoActionBar
而不是支持库主题.因此,充气机无法解析支持库的属性,例如?attr/actionBarSize
.
The cause of the error is that, in the toolbar xml, the ?attr
prefix refers to attributes in support library themes. On the other hand, under v22 of style.xml
above, the system theme android:Theme.Material.Light.NoActionBar
instead of a support library theme is used. The inflater therefore cannot resolve attributes of the support library such as ?attr/actionBarSize
.
在style.xml中使用支持库主题Theme.AppCompat.Light.NoActionBar
(与版本无关)可以解决此问题.
Using the support library theme Theme.AppCompat.Light.NoActionBar
in style.xml (regardless of version) addresses the issue.
这篇关于错误膨胀Android Lollipop中的类android.support.v7.widget.Toolbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!