SearchView和NavigationDrawer Android

时间:2014-11-12 11:34:50

标签: java android navigation-drawer searchview

我试图在我的应用程序中实现,其特点是导航抽屉,我想实现搜索视图。不幸的是,我得到了一个空值nell' oncreatemenu等...

我不明白我附加了错误的图像,日志cat和源代码。 提前谢谢,真诚相同的代码,但没有导航抽屉选项卡dell&action;操作栏很棒。我不明白,我试着在互联网上搜索但没有... 源代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_action, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(this);
    //searchView.setQueryHint("Inserisci qualcosa da cercare");
    return super.onCreateOptionsMenu(menu);
}

logcat的:

  

11-12 12:09:30.900 21373-21373 / com.lillonet.megadownloaderpro E /:设备驱动程序API匹配       设备驱动程序API版本:23       用户空间API版本:23   11-12 12:09:30.900 21373-21373 / com.lillonet.megadownloaderpro E /:mali:REVISION = Linux-r3p2-01rel3 BUILD_DATE = Wed Oct 9 21:05:57 KST 2013   11-12 12:10:01.740 21373-21373 / com.lillonet.megadownloaderpro E / AndroidRuntime:FATAL EXCEPTION:main       显示java.lang.NullPointerException               在com.lillonet.megadownloaderpro.NavActivity.onCreateOptionsMenu(NavActivity.java:344)               在android.app.Activity.onCreatePanelMenu(Activity.java:2578)               在android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)               在android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)               在android.support.v7.app.ActionBarActivityDelegate $ 1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)               在android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1056)               在android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1259)               在android.support.v7.app.ActionBarActivityDelegateBase.access $ 100(ActionBarActivityDelegateBase.java:80)               在android.support.v7.app.ActionBarActivityDelegateBase $ 1.run(ActionBarActivityDelegateBase.java:119)               在android.os.Handler.handleCallback(Handler.java:730)               在android.os.Handler.dispatchMessage(Handler.java:92)               在android.os.Looper.loop(Looper.java:176)               在android.app.ActivityThread.main(ActivityThread.java:5419)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:525)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)               在dalvik.system.NativeStart.main(本地方法)

enter image description here

XML CODE:

<item android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="ifRoom"
android:title="Cerca"
android:actionViewClass="android.widget.SearchView" />

1 个答案:

答案 0 :(得分:1)

android.widget.SearchView应改为android.support.v7.widget.SearchView并使用app前缀。你也应该在`app:showAsAction

上使用collapseActionView

像这样:

<item android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="collapseActionView"
android:title="Cerca"
app:actionViewClass="android.support.v7.widget.SearchView" />