我在我的Flickr应用程序中使用搜索视图,但是当我运行时,它只显示搜索视图的标题而不是它的搜索图标,当我点击标题时它应该显示和区域以键入搜索查询但是什么都没发生对待就像一个简单的工具栏项
这是菜单xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_item_search"
android:title="@string/search"
app:actionViewClass="android:support.v7.widget.searchView"
app:showAsAction="ifRoom"/>
<item android:id="@+id/menu_item_clear"
android:title="@string/clear_search"
app:showAsAction="never"/>
</menu>
这是模拟器中app的图片
当我引用代码并为搜索视图操作视图类设置文本侦听器时,它引用空引用错误
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_photo_gallery,menu);
MenuItem searchItem = menu.findItem(R.id.menu_item_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Log.d(TAG,"QueryTextSubmit: "+s);
updateItems();
return true;
}
@Override
public boolean onQueryTextChange(String s) {
Log.d(TAG,"queryTextChanged: "+s);
return true;
}
});
}
这是我崩溃后的logcat
09-05 15:04:25.759 14339-14339/com.bignerdranch.android.photogallery E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bignerdranch.android.photogallery, PID: 14339
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference
at com.bignerdranch.android.photogallery.PhotoGalleryFragment.onCreateOptionsMenu(PhotoGalleryFragment.java:71)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2291)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:3011)
at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:328)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:363)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1368)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1648)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:137)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
答案 0 :(得分:2)
作为Commented您需要编写android:support.v7.widget.SearchView
来解决相应的类命名约定
并添加collapseActionView|
以进行折叠功能
<item
android:id="@+id/menu_item_search"
android:title="@string/search"
app:actionViewClass="android:support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
答案 1 :(得分:1)
我已将appcompact库的版本更改为25.3.0并解决了
我认为图书馆已损坏。
这是我的gradle的代码
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:25.3.0'
}
答案 2 :(得分:0)
更改此使用android:support.v7.widget.SearchView
instedof android:support.v7.widget.searchView
<item
android:id="@+id/menu_item_search"
android:title="@string/search"
app:actionViewClass="android:support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>