android - 工具栏在 Intent/AppCompatActivity 中的菜单膨胀时给出 fatal error

标签 android android-layout toolbar menuitem android-include

在我的应用程序中,我有一个工具栏,它在我的 MainActivity 中运行得非常好。

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/toolbar_item_counter"
        android:id="@+id/toolbar_tv_itemCounter"
        android:textSize="18dp"
        android:textColor="#ffffff"
        android:textStyle="bold"/>
</RelativeLayout>

但是当我尝试膨胀这个菜单时

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/item_accept"
    android:title="Accept"
    android:icon="@drawable/ic_action_accept"
    app:showAsAction="always"/>
</menu>

用这段代码

public class AddActivity extends AppCompatActivity {

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.inflateMenu(R.menu.menu_input_accept);

    TextView toolbartext = (TextView) findViewById(R.id.toolbar_tv_itemCounter);
    toolbartext.setVisibility(View.INVISIBLE);

}

它给了我这个

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.inflateMenu(int)' on a null object reference

我不明白的 fatal error 。我还尝试覆盖 onCreateOptionsMenu 方法并像在 MainActivity 中那样使用 MenuInflater,但它没有任何改变。我在 xml 中包含了我的工具栏,就像我在主要 Activity 中所做的那样:

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

最佳答案

I included my toolbar in the xml like I did in my main activity:

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

当你包含一个布局时,如果你为 include 标签提供一个 id(就像你所做的那样,通过使用 android:id="@+id/include")那么被包含的根layout 将具有该 id,覆盖已在包含的 layout 中分配的任何 id。因此,在您的情况下,工具栏的 ID 为 R.id.include 而不是 R.id.toolbar。

要么删除 android:id="@+id/include",将其更改为 android:id="@+id/toolbar" 要么寻找工具栏使用 findViewById(R.id.include)

关于android - 工具栏在 Intent/AppCompatActivity 中的菜单膨胀时给出 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559468/

相关文章:

android - 更新 SDK 后,Pixel 启动器一直停止

java - localStorage 不会在使用 Java 8 重建 Ionic (Cordova) 应用程序时持续存在

android - Fragments 什么时候有非空的 savedInstanceState

android - 工具栏下的 fragment

c++ - MFC:如何在旧版 MFC ver 6 中保存工具栏的位置?

android - 如何找到地理点之间的距离?

android - 在 Android 应用程序的 REST 服务请求期间显示加载屏幕?

java - 拆分操作栏不透明

java - Android Studio 中的批量编辑 Activity

android - 如何处理 SearchView 中的后退箭头事件