I have an activity which leverages Navigation drawer and I have three fragment children attached in this. I have an Action Bar search implemented, while as everything is working fine on a 5.0 device, I am facing hiccups on a 4.0.3 device on one of the fragments. I am implementing search as follows:
SearchView searchView;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
searchView = (SearchView)menu.findItem(R.id.search).getActionView();
searchView.setSubmitButtonEnabled(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText){
if (TextUtils.isEmpty(newText)){
CursorAdapter filterAdapter = (CursorAdapter) Cur;
filterAdapter.getFilter().filter(newText.toString());
searchView.clearFocus();
}
else {
CursorAdapter filterAdapter = (CursorAdapter) Cur;
filterAdapter.getFilter().filter(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
};
Cur.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.getSearchList(constraint);
}
});searchView.setOnQueryTextListener(textChangeListener);
super.onCreateOptionsMenu(menu,inflater);
}
At the start the first fragment works fine, when I use the Navigation drawer to select a different fragment my app crashes, here is the log cat:
11-25 17:00:17.554: E/AndroidRuntime(6667): java.lang.NullPointerException
11-25 17:00:17.554: E/AndroidRuntime(6667): at com.exa.FragmentTab2.onCreateOptionsMenu(FragmentTab2.java:4477)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1871)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.Activity.onCreatePanelMenu(Activity.java:2489)
11-25 17:00:17.554: E/AndroidRuntime(6667): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:395)
11-25 17:00:17.554: E/AndroidRuntime(6667): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:760)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.Activity.invalidateOptionsMenu(Activity.java:2607)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.FragmentManagerImpl.invalidateOptionsMenu(FragmentManager.java:1950)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.Fragment.setHasOptionsMenu(Fragment.java:901)
11-25 17:00:17.554: E/AndroidRuntime(6667): at com.exa.FragmentTab2.onCreateView(FragmentTab2.java:298)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.BackStackRecord.run(BackStackRecord.java:635)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.os.Handler.handleCallback(Handler.java:615)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.os.Looper.loop(Looper.java:153)
11-25 17:00:17.554: E/AndroidRuntime(6667): at android.app.ActivityThread.main(ActivityThread.java:5000)
11-25 17:00:17.554: E/AndroidRuntime(6667): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 17:00:17.554: E/AndroidRuntime(6667): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 17:00:17.554: E/AndroidRuntime(6667): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
11-25 17:00:17.554: E/AndroidRuntime(6667): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
11-25 17:00:17.554: E/AndroidRuntime(6667): at dalvik.system.NativeStart.main(Native Method)
in OnCreate the log refers to this statement:
setHasOptionsMenu(true);
in the onCreateOptionsMenu the log refers to:
Cur.setFilterQueryProvider(new FilterQueryProvider()
I am very sure that my Cursor is not null.
EDIT
I removed the
Cur.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.getSearchList(constraint);
}
});
to onCreate and now I am getting a null pointer at:
filterAdapter.getFilter().filter(newText.toString());
I am even more sure that you are incorrect. The only way to get a
NullPointerException
on that line is ifCur
isnull
.Please move your
setFilterQueryProvider()
to a better place, such as where you assignCur
a value. Note that yoursetFilterQueryProvider()
does not need to be inonCreateOptionsMenu()
, as it has nothing to do withonCreateOptionsMenu()
(e.g., it does not need theMenu
orMenuInflater
objects).