今天我花了时间迁移到了AppCompat库。除了我的SearchView之外,我已经成功设置了一切。
在此之前,它功能正常,但现在当我使用以下代码时,我不断收到nullPointerException
:
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
我已经到处寻找解决方案来解决我的问题,但我找不到一个(我尝试过的方法都不起作用)。
如果有任何指导,将不胜感激。
这是我调用的地方:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(searchItem);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
这是菜单的XML文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res/android" >>
<item
android:id="@+id/about"
android:orderInCategory="3"
android:showAsAction="never"
android:title="@string/about"/>
<item
android:id="@+id/rate"
android:orderInCategory="2"
android:showAsAction="never"
android:title="@string/rate"/>
<item
android:id="@+id/search"
android:icon="@android:drawable/ic_menu_search"
android:orderInCategory="1"
app:actionViewClass="android.support.v7.widget.SearchView"
compat:showAsAction="ifRoom|collapseActionView">
</item>
清单:
<activity android:name="com.stack.overflow.MainActivity" android:label="@string/app_name" android:launchMode="singleTop" android:theme="@style/Theme.AppCompat" > <meta-data android:name="android.app.default_searchable" android:resource="@xml/searchable" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.SEARCH" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
FATAL EXCEPTION: main java.lang.NullPointerException at com.stack.overflow.MainActivity.onCreateOptionsMenu(MainActivity.java:805) at android.app.Activity.onCreatePanelMenu(Activity.java:2476) at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224) at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232) at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146) at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199) at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293) at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:393) at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747) at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2913) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)