在 android 中添加工具栏时出现 InflateException

InflateException when adding toolbar in android

我是 material 设计的新手,我正在尝试一些东西。 对于我的应用程序,最低 sdk 是 21。但是我的应用程序因以下代码而崩溃。

工具栏代码是

<?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="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:background="@color/colorPrimary"
    android:elevation="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
</android.support.v7.widget.Toolbar>



</LinearLayout>

主要activity的代码

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.sagar.moviesuccesspredictor.MainActivity">
<include
    android:id="@+id/tool_bar"
    android:layout="@layout/tool_bar"
    tools:layout_editor_absoluteY="45dp"
    tools:layout_editor_absoluteX="0dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
      android:layout_below="@+id/tool_bar"
        android:layout_marginTop="10dp"/>

</RelativeLayout>

我的 java 代码是

public class MainActivity extends AppCompatActivity  {
Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=(android.support.v7.widget.Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
    }
}

我不需要任何 Lolipop 之前的向后兼容性。 如果没有向后支持,我也找不到任何教程。 任何帮助对我来说都是很好的。

logcat 是

Unable to start activity ComponentInfo{com.example.sagar.moviesuccesspredictor/com.example.sagar.moviesuccesspredictor.MainActivity}: android.view.InflateException: 
Binary XML file line #9: You must specify a layout in the include tag: <include layout="@layout/layoutID"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3320) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
at android.app.ActivityThread.access00(ActivityThread.java:230)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #9: You must specify a layout in the include tag: <include layout="@layout/layoutID" />
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.sagar.moviesuccesspredictor.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3267)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
at android.app.ActivityThread.access00(ActivityThread.java:230) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7409) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: android.view.InflateException: You must specify a layout in the include tag: <include layout="@layout/layoutID" />
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:923)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:843)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.example.sagar.moviesuccesspredictor.MainActivity.onCreate(MainActivity.java:16) 
at android.app.Activity.performCreate(Activity.java:6904) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3267) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
at android.app.ActivityThread.access00(ActivityThread.java:230) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7409) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

试试这个。

你的toolbar.xml应该是这样的。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tool_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >

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

试试这个代码

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

并在 style.xml

中添加这些行
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

您的工具栏 ID 是 toolbar 而不是 tool_bar。使用喜欢,

public class MainActivity extends AppCompatActivity  {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar=(Toolbar) findViewById(R.id.toolbar);

  }
}

编辑 1:使用工具栏的示例

toolbar.xml

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/app_name"
    android:textColor="#fff"
    android:gravity="center"
    android:textStyle="bold"
    android:textSize="18dp"/>

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

main_activity.xml

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

<include
    android:id="@+id/this_toolbar"
    layout="@layout/toolbar"/>

<TextView
    android:id="@+id/toolbar_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/this_toolbar"
    android:text="TextView"/>

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

TextView m_toolbar_title;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    m_toolbar_title = (TextView)findViewById(R.id.toolbar_title);
    m_toolbar_title.setText("Activity Title");

    }

}

你在activity_main.xml中的layout="@layout/toolbar"toolbar应该是file_name,它有<Toolbar>

看看吧。这可能对你有帮助。

将您的主要 activity xml 更改为此

 <?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.sagar.moviesuccesspredictor.MainActivity">
<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar"
    tools:layout_editor_absoluteY="45dp"
    tools:layout_editor_absoluteX="0dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
      android:layout_below="@+id/tool_bar"
        android:layout_marginTop="10dp"/>

</RelativeLayout>

问题出在标记而不是 android:layout 中,您必须使用 layout:yourLayoutfile。并确保在 mainActivity 中也使用支持工具栏

打开logcat,可能会提示Toolbar与已有的ActionBar冲突。解决办法是修改AndroidManifest.xml

<activity
            android:name=".ToolbarActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.Design.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

添加没有默认 ActionBar 的主题。