如何在操作栏中实现默认搜索查看

时间:2015-02-25 09:23:10

标签: java android android-actionbar

我正在行动栏中实施searchview,但我收到错误,我可能知道我做错了。请纠正我

    enter code here
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.overview_menu_with_search, menu);

    SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(this);

    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.logout:
        session.logoutUser();
        finish();
        return true;

    case android.R.id.home:
        this.finish();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }

}

@Override
public boolean onQueryTextChange(String newText) {

    if (TextUtils.isEmpty(newText))
      {
            lv.clearTextFilter();
        }
      else
      {
            lv.setFilterText(newText.toString());
        }

      return true;
}

@Override
public boolean onQueryTextSubmit(String query) {

    return false;
}

这是我的错误代码..

02-23 19:17:02.179: E/AndroidRuntime(435): java.lang.NullPointerException
02-23 19:17:02.179: E/AndroidRuntime(435):  at com.example.example.Activity5.onCreateOptionsMenu(abc.java:411)
02-23 19:17:02.179: E/AndroidRuntime(435):  at android.app.Activity.onCreatePanelMenu(Activity.java:2546)
02-23 19:17:02.179: E/AndroidRuntime(435):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
02-23 19:17:02.179: E/AndroidRuntime(435):  at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
02-23 19:17:02.179: E/AndroidRuntime(435):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
02-23 19:17:02.179: E/AndroidRuntime(435):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-23 19:17:02.179: E/AndroidRuntime(435):  at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-23 19:17:02.179: E/AndroidRuntime(435):  at android.view.Choreographer.doFrame(Choreographer.java:543)
02-23 19:17:02.179: E/AndroidRuntime(435):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-23 19:17:02.179: E/AndroidRuntime(435):  at android.os.Handler.handleCallback(Handler.java:733)
02-23 19:17:02.179: E/AndroidRuntime(435):  at android.os.Handler.dispatchMessage(Handler.java:95)
02-23 19:17:02.179: E/AndroidRuntime(435):  at android.os.Looper.loop(Looper.java:136)
02-23 19:17:02.179: E/AndroidRuntime(435):  at android.app.ActivityThread.main(ActivityThread.java:5086)
02-23 19:17:02.179: E/AndroidRuntime(435):  at java.lang.reflect.Method.invokeNative(Native Method)
02-23 19:17:02.179: E/AndroidRuntime(435):  at java.lang.reflect.Method.invoke(Method.java:515)
02-23 19:17:02.179: E/AndroidRuntime(435):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-23 19:17:02.179: E/AndroidRuntime(435):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-23 19:17:02.179: E/AndroidRuntime(435):  at dalvik.system.NativeStart.main(Native Method) 

0 个答案:

没有答案