Android 5.0中使用AppCompat的SearchView上的NullPointerException

我正在使用所有AppCompat主题更新我的应用程序。 我在这里得到一个空指针:

@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); MenuItem searchItem = menu.findItem(R.id.menu_search); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); setupNewSearchView(searchItem, searchManager); return true; } private void setupNewSearchView(final MenuItem searchItem, SearchManager searchManager) { final SearchView searchView = (SearchView) searchItem.getActionView(); // Null at line below searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); // Everything else below edited out } 

这是我的关键import:

 import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.SearchView; 

我是否认为在更新到AppCompat时,必须更改此行?

 final SearchView searchView = (SearchView) searchItem.getActionView(); 

更新

我改变了这一点

  final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); 

相同的空结果…

更新 – 添加XML

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/menu_search" app:actionViewClass="android.support.v7.widget.SearchView" android:icon="@android:drawable/ic_menu_search" app:showAsAction="always" android:title="search" /> <item </menu> 

LogCat

 10-24 11:49:07.040: E/AndroidRuntime(4846): java.lang.NullPointerException 10-24 11:49:07.040: E/AndroidRuntime(4846): at com.---.---.MainFragmentActivity.setupNewSearchView(MainFragmentActivity.java:1300) 10-24 11:49:07.040: E/AndroidRuntime(4846): at com.---.---.MainFragmentActivity.onCreateOptionsMenu(MainFragmentActivity.java:1292) 10-24 11:49:07.040: E/AndroidRuntime(4846): at android.app.Activity.onCreatePanelMenu(Activity.java:2538) 10-24 11:49:07.040: E/AndroidRuntime(4846): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275) 10-24 11:49:07.040: E/AndroidRuntime(4846): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276) 10-24 11:49:07.040: E/AndroidRuntime(4846): at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79) 10-24 11:49:07.040: E/AndroidRuntime(4846): at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1056) 10-24 11:49:07.040: E/AndroidRuntime(4846): at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1259) 

Solutions Collecting From Web of "Android 5.0中使用AppCompat的SearchView上的NullPointerException"

与支持库你必须使用一个特殊的菜单项类: MenuItemCompat

 android.support.v4.view.MenuItemCompat 

方法名称( getActionView )保持不变。

你的菜单xml应该是这样的:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_search" android:icon="@android:drawable/ic_menu_search" app:showAsAction="always" app:actionViewClass="android.support.v7.widget.SearchView" android:title="Search"/> </menu>