【问题标题】:Android.Views.InflateException : Binary XML file line #1: Error inflating class toolbarAndroid.Views.InflateException:二进制 XML 文件第 1 行:膨胀类工具栏时出错
【发布时间】:2017-02-11 06:50:32
【问题描述】:

我对这段代码有疑问。当我在 kitkat 中运行它时,它会提示一个未处理的异常

Android.Views.InflateException:二进制 XML 文件第 1 行:膨胀类工具栏时出错

顺便说一句:我也用过

...

android.support.v7.widget.Toolbar >

正如其他问题中所建议的,但它仍然是一样的。

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

    <!--Error came from this toolbar-->

    <Toolbar 
        android:minHeight="30dp"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="@color/lime"
        android:id="@+id/toolbar"
        android:elevation="6dp">
        <TextView
            android:text="BranchName"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginRight="155dp"
            android:id="@+id/branchName"
            android:textColor="#000000" />
        <TextView
            android:text="Logout"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:id="@+id/logout"
            android:layout_marginRight="5dp"
            android:elevation="8dp"
            android:textColor="#000000" />
    </Toolbar>
</LinearLayout>

它包含在其他 .axml 中

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px"
        android:background="#fafafa"
        android:weightSum="100">

        <!--Toolbar was called from here by include-->

        <include 
            layout="@layout/ToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:orientation="horizontal"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/olive"
            android:id="@+id/linearLayout1"
            android:padding="5dp">
            <ImageButton
                android:src="@drawable/backB"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="@color/olive"
                android:id="@+id/backButton"
                android:scaleType="fitCenter" />
            <LinearLayout
                android:orientation="vertical"
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout3"
                android:layout_marginLeft="-30dp">
                <TextView
                    android:text="My Orders"
                    android:textAppearance="?                                                                                                          android:attr/textAppearanceLarge"
                    android:textColor="@color/white"
                    android:id="@+id/textView1"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </LinearLayout>

        <!--Other codes are not related anymore to the issue-->

    </LinearLayout>

我研究了很多,但我认为有关 InflateException 的其他问题与我的问题无关。 我应该在这里解决什么问题?

【问题讨论】:

标签: android xml xamarin.android


【解决方案1】:

经过大量测试,我成功了 :D 谢谢 https://devblogs.microsoft.com/xamarin/android-tips-hello-appcompatactivity-goodbye-actionbaractivity/

这是我的改动

我添加了 nuget xamarin.android.support.v7.appCompat 及其依赖项。 (因为 xamarin 没有 gradle)

*在activity.cs中:

-使用Android.Support.V7.App;

-来自公共类 OrdersActivity : Activity

-到公共类 OrdersActivity : AppCompatActivity

*在 style.xml 中

-from style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar"

-to style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar"

*在 ToolbarLayout.axml 中

-来自

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Toolbar
        android:minHeight="30dp"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="@color/lime"
        android:id="@+id/toolbar"
        android:elevation="6dp">

      ...
   
  </Toolbar>
</LinearLayout>

-到

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

    <!--I change it back to android.support.v7.widget-->

    <android.support.v7.widget.Toolbar
        android:minHeight="30dp"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="@color/lime"
        android:id="@+id/toolbar"
        android:elevation="6dp">

      ...
   
  </android.support.v7.widget.Toolbar>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 2016-11-16
    • 1970-01-01
    • 2016-01-12
    相关资源
    最近更新 更多