Problem Descrption:
Caused by: android.view.InflateException: You must specifiy a layout in the include tag:<include layout="@layout/layoutID"/>"
Solution 1:
I find answer to my own question. In my tool activity_main
xml layout i replaced android:layout="@layout/tool_bar"
with layout="@layout/tool_bar"
and in tool bar layout android.widget.Toolbar
with android.support.v7.widget.Toolbar
Check out below my both xml files
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/tool_bar"
android:layout="@layout/tool_bar" />
</RelativeLayout>
Corrected and working xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
</RelativeLayout>
Solution 2:
layout="@layout/view_toolbar"
instead:
android:layout="@layout/view_toolbar
Solution 3:
In my case I added a layout with the correct attribute (layout="@layout/digits_layout"
), but Android Studio
kept throwing this annoying exception. I pressed File
> Invalidate and restart
, Build
> Rebuild Project
. Also removed <include>
tag from XML. Only after that the exception disappeared. So I suppose only rebuild helps to avoid the problem if you already threw out the <include>
tag.So, in a fragment a have a layout like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<include
layout="@layout/digits_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
...
</android.support.constraint.ConstraintLayout>
digits_layout
also contains included views:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
>
<include
android:id="@+id/digit_1"
layout="@layout/item_digit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
/>
...
I think that was the reason.I use Kotlin with Kotlin Android Extensions
for importing resources like import kotlinx.android.synthetic.main.fragment_my.*,
that simplifies accessing to views (no findViewById()
needed). In my project I separated resources (drawables
, layouts
) by folders. Maybe it matters. Replacing <include>
with <merge>
as in solution didn’t help. Also accessing to any view inside this included layout made it null.
My own:
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<include
layout="@layout/top_actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#ffffff"/>
<!-- pin 无论下面的页面怎么滑动始终顶部栏 /*androidx.appcompat.widget.Toolbar-->
</com.google.android.material.appbar.AppBarLayout>
solution:我的主要是删掉了注释//
,ctr+/注释。起初尝试了很多的解决方法,添加/去掉android:
都没用。我这应该就是注释问题