在Android Lollipop中输入类android.support.v7.widget.Toolbar时出错

时间:2015-09-12 15:45:00

标签: android android-5.0-lollipop android-support-library android-styles

我为我的项目使用android.support库,我使用android 4.4开发了我的应用程序并且一切正常但我的问题出现在我尝试在Android Lollipop设备中运行应用程序时。

当它使用工具栏加载Activity时,应用程序将停止并返回此错误。

错误导致类android.support.v7.widget.Toolbar错误

我在互联网上发现了同样的问题,但我没有在答案中找到解决方案。

这是我的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)

<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中定义工具栏

<?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中,我只包含该工具栏:

<include layout="@layout/toolbar" />

最后是我的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)

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:9)

错误原因是,在工具栏xml中,?attr前缀是指支持库主题中的属性。另一方面,在上面style.xml的v22下,使用系统主题android:Theme.Material.Light.NoActionBar而不是支持库主题。因此,inflater无法解析支持库的属性,例如?attr/actionBarSize

使用style.xml中的支持库主题Theme.AppCompat.Light.NoActionBar(无论版本如何)解决了该问题。

答案 1 :(得分:0)

在我的情况下相同的错误,但在android axml文件中没有?attr的症状

它看起来像是Xamarin.Appcombat的兼容性问题(我在网上找到了某个地方)

尝试将同一项目部署到较低的Android API

在我的情况下,Android模拟器API 21出现上述错误,但另一个使用Android API 18的模拟器完全没有问题