使用简单工具栏时如何修复android.view.InflateException

时间:2018-06-25 04:33:17

标签: android android-layout android-studio android-toolbar android-styles

我正在使用Android Studio。我没有使用自定义工具栏。只需将其添加到android应用程序即可。我在MainActivity中扩展AppCompatActivity。我在堆栈溢出中尝试了多种无效的解决方案。我认为这是因为我的毕业证书是最新的。

这是错误日志:

06-21 12:23:30.471 1455-1455/com.kelldavis.movieguide E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kelldavis.movieguide, PID: 1455
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kelldavis.movieguide/com.kelldavis.movieguide.ui.MainActivity}: android.view.InflateException: Binary XML file line #10: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x10102eb a=2 r=0x7f070051}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2690)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2755)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1495)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6196)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
 Caused by: android.view.InflateException: Binary XML file line #10: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x10102eb a=2 r=0x7f070051}
 Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x10102eb a=2 r=0x7f070051}
    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:716)
    at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7247)
    at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:439)
    at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:370)
    at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:369)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:860)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.kelldavis.movieguide.ui.MainActivity.onCreate(MainActivity.java:52)
    at android.app.Activity.performCreate(Activity.java:6698)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2643)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2755)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1495)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6196)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

MainActivity中的这一行是日志中抛出的内容:

        setContentView(R.layout.activity_main);

这是我的activity_main.xml文件:

    <FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="ui.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/main_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/attr_action_bar_size" />

    <include layout="@layout/toolbar_w_shadow" />
</FrameLayout>

这是我的toolbar_w_shadow.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_elevated"
        android:layout_width="match_parent"
        android:layout_height="@dimen/attr_action_bar_size"
        android:background="@color/attrColorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@drawable/shadow" />
</LinearLayout>

根据要求,这是我的操作栏尺寸的尺寸值:

<dimen name="attr_action_bar_size">?android:attr/actionBarSize</dimen>

3 个答案:

答案 0 :(得分:0)

  

在代码下方的清单文件中进行更改将使主题类似于以下代码,并将重力顶部设置为工具栏布局。

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
</activity>

答案 1 :(得分:0)

以此更改您的toolbar_w_shadow.xml代码。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

   <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_elevated"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/attrColorPrimary"
            app:theme="@style/Theme.AppCompat.NoActionBar" />


    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@drawable/shadow" />
</LinearLayout>

答案 2 :(得分:0)

您已从生成异常的 dimens.xml 文件中将其设置为?android:attr / actionBarSize

解决方案

  1. 直接设置它,不要从其他资源文件中获取它

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/main_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?android:attr/actionBarSize" />

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

    </FrameLayout>

toolbar_w_shadow.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_elevated"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/attrColorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:background="@drawable/shadow" />

    </LinearLayout>

2。从styles.xml设置

    <style name="myToolbarHeight" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:layout_height">?android:attr/actionBarSize</item>
    </style>

    <style name="myToolbarTopMargin" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:layout_marginTop">?android:attr/actionBarSize</item>
    </style>

android:layout_height =“?android:attr / actionBarSize” 替换为 style =“ @ style / myToolbarHeight” android:layout_marginTop =“?android :attr / actionBarSize“ style =” @ style / myToolbarTopMargin“