Home » Android » android – How to fix ' You must specifiy a layout in the include tag: <include layout="@layout/layoutID" />'

android – How to fix ' You must specifiy a layout in the include tag: <include layout="@layout/layoutID" />'

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am creating an android application using material design in android studio for pre lollipop devices and i am new to android studio i am creating a tool bar in my project but i am getting an error called ” Caused by: android.view.InflateException: You must specifiy a layout in the include tag: “ please help me to solve this, This is my activity code:

package sample.lakshman.com.sampleltester;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;


public class MainActivity extends ActionBarActivity {
    public Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is my 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"
        android:layout="@layout/tool_bar" />
</RelativeLayout>

This is my Toolbar Xml:

  <?xml version="1.0" encoding="utf-8"?>
    <android.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#E91E63">

    </android.widget.Toolbar>


This is my logcat:
 03-06 13:09:39.313  17890-17890/sample.lakshman.com.sampleltester E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: sample.lakshman.com.sampleltester, PID: 17890
        java.lang.RuntimeException: Unable to start activity ComponentInfo{sample.lakshman.com.sampleltester/sample.lakshman.com.sampleltester.MainActivity}: android.view.InflateException: You must specifiy a layout in the include tag: <include layout="@layout/layoutID" />
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                at android.os.Handler.dispatchMessage(Handler.java:110)
                at android.os.Looper.loop(Looper.java:193)
                at android.app.ActivityThread.main(ActivityThread.java:5292)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: android.view.InflateException: You must specifiy a layout in the include tag: <include layout="@layout/layoutID" />
                at android.view.LayoutInflater.parseInclude(LayoutInflater.java:787)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
                at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
                at sample.lakshman.com.sampleltester.MainActivity.onCreate(MainActivity.java:16)
                at android.app.Activity.performCreate(Activity.java:5264)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                at android.os.Handler.dispatchMessage(Handler.java:110)
                at android.os.Looper.loop(Looper.java:193)
                at android.app.ActivityThread.main(ActivityThread.java:5292)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                at dalvik.system.NativeStart.main(Native Method)
How to&Answers:

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.Tool bar
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>

For refrernce see the answer here

Answer:

layout="@layout/view_toolbar" 

instead

android:layout="@layout/view_toolbar

Answer:

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 https://developer.android.com/training/improving-layouts/reusing-layouts didn’t help. Also accessing to any view inside this included layout made it null.