Java android中使用menu.xml中的actionLayout时出现NullPointerException

Java android中使用menu.xml中的actionLayout时出现NullPointerException,java,android,xml,nullpointerexception,Java,Android,Xml,Nullpointerexception,我想在我的应用程序的菜单中使用自定义SearchView,但在android中使用menu.xml中的actionLayout时遇到NullPointerException 我有菜单的自定义布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_

我想在我的应用程序的菜单中使用自定义SearchView,但在android中使用menu.xml中的actionLayout时遇到NullPointerException 我有菜单的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
    android:id="@+id/search_btn"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@android:drawable/ic_menu_search"/>

<EditText
    android:id="@+id/search_et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/search_btn"
    android:layout_toLeftOf="@+id/search_btn"
    android:ems="10"
    android:inputType="none" >

    <requestFocus />
</EditText>

但我在上面提到的行中得到了NullPointerException。如何将ClickListener添加到该按钮?

您使用了错误的id。请使用菜单项的id。您可以在菜单项上使用getActionView来获取布局

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.menu, menu);

    MenuItem searchItem = menu.findItem(R.id.search_view);
    Button searchButton = searchItem.getActionView().findViewById(R.id.search_btn);
    searchButton.setOnClickListener(new OnClickListener() { // SEE HERE I'M GETTING    NullPointerException 
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, ((EditText)    findViewById(R.id.search_et)).getText().toString(), Toast.LENGTH_LONG).show();

        }
    });
   return true;
}

您使用了错误的id。请使用菜单项的id。您可以在菜单项上使用getActionView来获取布局

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.menu, menu);

    MenuItem searchItem = menu.findItem(R.id.search_view);
    Button searchButton = searchItem.getActionView().findViewById(R.id.search_btn);
    searchButton.setOnClickListener(new OnClickListener() { // SEE HERE I'M GETTING    NullPointerException 
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, ((EditText)    findViewById(R.id.search_et)).getText().toString(), Toast.LENGTH_LONG).show();

        }
    });
   return true;
}

嗯,你没有得到你的按钮的参考。i、 e在
searchButton=(按钮)菜单中.findItem(R.id.search\u btn)搜索按钮为空


您使用了一个错误的id。

好吧,您没有获得对按钮的引用。i、 e在
searchButton=(按钮)菜单中.findItem(R.id.search\u btn)搜索按钮为空


您使用了错误的id。

请尝试OnOptions ItemSelected方法

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.search_view:
        //write your code here
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

请尝试OnOptions ItemSelected方法

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.search_view:
        //write your code here
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

你为什么不使用
onOptionsItemSelected
?对不起,我不能得到,请你给我一些代码Snnip:-Pdo你有回溯吗?你为什么不使用
onOptionsItemSelected
?对不起,我不能得到,请给我一些代码Snnip:-Pdo你有回溯吗?你的答案非常有用!即使在使用NavigationView的情况下,我们也可以这样做(使用onCreate()方法而不是onCreateOptions菜单()。我们只需要以另一种方式获取对MenuItem的引用:
NavigationView NavigationView=this.findViewById(R.id.navigation\u视图);MenuItem searchItem=navigation.getMenu().findItem(R.id.search_视图);//你的答案非常有用!即使在使用NavigationView的情况下,我们也可以这样做(使用onCreate()方法而不是onCreateOptions菜单()。我们只需要以另一种方式获取对MenuItem的引用:
NavigationView NavigationView=this.findViewById(R.id.navigation\u视图);MenuItem searchItem=navigation.getMenu().findItem(R.id.search_视图);//