在Android 5.0中使用AppCompat的SearchView上的NullPointerException

前端之家收集整理的这篇文章主要介绍了在Android 5.0中使用AppCompat的SearchView上的NullPointerException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用所有AppCompat主题更新我的应用程序.我在这里得到一个nullpointer:
@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 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)

解决方法

使用支持库,您必须使用一个特殊的菜单项类: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>

猜你在找的Android相关文章