【问题标题】:How to fix ' You must specifiy a layout in the include tag: <include layout="@layout/layoutID" />'如何修复'您必须在包含标签中指定布局:<include layout="@layout/layout ID" />'
【发布时间】:2015-03-06 07:41:20
【问题描述】:

我正在使用 android studio 中的材料设计为 pre lollipop 设备创建一个 android 应用程序,我是 android studio 的新手,我正在我的项目中创建一个工具栏,但我收到一个名为 的错误 原因:android .view.InflateException: You must specify a layout in include tag: " 请帮我解决这个问题,这是我的活动代码:

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);
    }
}

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

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

【问题讨论】:

  • 这是迄今为止 Android Studio 中最烦人的恼人错误。警告我们这一点并没有什么坏处。
  • @NeonWarge,是的,很烦人。我花了几个小时在它上面并从布局中删除了 。

标签: android android-layout toolbar


【解决方案1】:

我找到了自己问题的答案。在我的工具 activity_main xml 布局中,我将 android:layout="@layout/tool_bar" 替换为 layout="@layout/tool_bar" 并在工具栏布局中将 android.widget.Toolbar 替换为 android.support.v7.widget.Tool bar 查看下面我的两个 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>

已更正并正常工作的 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>

参考答案here

【讨论】:

    【解决方案2】:
    layout="@layout/view_toolbar" 
    

    改为

    android:layout="@layout/view_toolbar
    

    【讨论】:

      【解决方案3】:

      在我的例子中,我添加了一个具有正确属性 (layout="@layout/digits_layout") 的布局,但 Android Studio 一直抛出这个烦人的异常。我按了File &gt; Invalidate and restart、Build &gt; Rebuild Project。还从 XML 中删除了 &lt;include&gt; 标记。只有在那之后,异常才消失。所以我想如果你已经扔掉了&lt;include&gt;标签,只有重建有助于避免这个问题。

      因此,在片段中,a 的布局如下:

      <?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 还包含包含的视图:

      <?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"
              />
      
          ...
      

      我认为这就是原因。

      我使用 Kotlin 和 Kotlin Android Extensions 来导入像 import kotlinx.android.synthetic.main.fragment_my.* 这样的资源,这简化了对视图的访问(不需要 findViewById())。在我的项目中,我按文件夹分隔资源(可绘制对象、布局)。也许这很重要。用&lt;merge&gt; 替换&lt;include&gt;,如https://developer.android.com/training/improving-layouts/reusing-layouts 并没有帮助。还可以访问此包含布局中的任何视图,使其成为null。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 2017-06-21
        • 2016-07-18
        • 2022-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多