Prevent NPE

pull/214/head
M66B 11 months ago
parent 2e409a5dd4
commit dfa35d4da4

@ -271,25 +271,26 @@ public class FragmentAnswers extends FragmentBase {
} }
}); });
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { if (searchView != null)
@Override searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) { @Override
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { public boolean onQueryTextChange(String newText) {
searching = newText; if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
adapter.search(newText); searching = newText;
adapter.search(newText);
}
return true;
} }
return true;
}
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
searching = query; searching = query;
adapter.search(query); adapter.search(query);
}
return true;
} }
return true; });
}
});
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String sort = prefs.getString("answer_sort", "order"); String sort = prefs.getString("answer_sort", "order");

@ -374,25 +374,26 @@ public class FragmentContacts extends FragmentBase {
} }
}); });
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { if (searchView != null)
@Override searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) { @Override
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { public boolean onQueryTextChange(String newText) {
searching = newText; if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
adapter.search(newText); searching = newText;
adapter.search(newText);
}
return true;
} }
return true;
}
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
searching = query; searching = query;
adapter.search(query); adapter.search(query);
}
return true;
} }
return true; });
}
});
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
} }

@ -669,25 +669,26 @@ public class FragmentFolders extends FragmentBase {
} }
}); });
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { if (searchView != null)
@Override searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) { @Override
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { public boolean onQueryTextChange(String newText) {
searching = newText; if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
adapter.search(newText); searching = newText;
adapter.search(newText);
}
return true;
} }
return true;
}
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
searching = query; searching = query;
adapter.search(query); adapter.search(query);
}
return true;
} }
return true; });
}
});
LayoutInflater infl = LayoutInflater.from(getContext()); LayoutInflater infl = LayoutInflater.from(getContext());
ImageButton ibSearch = (ImageButton) infl.inflate(R.layout.action_button, null); ImageButton ibSearch = (ImageButton) infl.inflate(R.layout.action_button, null);

@ -336,127 +336,130 @@ public class FragmentOptions extends FragmentBase {
} }
}; };
searchView.setOnSuggestionListener(onSuggestionListener); if (searchView != null)
searchView.setOnSuggestionListener(onSuggestionListener);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { if (searchView != null)
@Override searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextSubmit(String query) { @Override
searching = query; public boolean onQueryTextSubmit(String query) {
searching = query;
CursorAdapter adapter = searchView.getSuggestionsAdapter(); CursorAdapter adapter = searchView.getSuggestionsAdapter();
if (adapter != null && adapter.getCount() > 0) if (adapter != null && adapter.getCount() > 0)
onSuggestionListener.onSuggestionClick(0); onSuggestionListener.onSuggestionClick(0);
return false; return false;
} }
@Override
public boolean onQueryTextChange(String newText) {
if (newText != null)
newText = newText.trim();
searching = newText;
suggest(newText);
return false;
}
private void suggest(String query) { @Override
Bundle args = new Bundle(); public boolean onQueryTextChange(String newText) {
args.putString("query", query); if (newText != null)
newText = newText.trim();
searching = newText;
suggest(newText);
return false;
}
new SimpleTask<SuggestData>() { private void suggest(String query) {
@Override Bundle args = new Bundle();
protected SuggestData onExecute(Context context, Bundle args) { args.putString("query", query);
if (TextUtils.isEmpty(args.getString("query")))
return data;
return (data == null ? getSuggestData(context) : data); new SimpleTask<SuggestData>() {
} @Override
protected SuggestData onExecute(Context context, Bundle args) {
if (TextUtils.isEmpty(args.getString("query")))
return data;
@Override return (data == null ? getSuggestData(context) : data);
protected void onExecuted(Bundle args, SuggestData result) { }
data = result;
_suggest(args.getString("query"));
}
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onExecuted(Bundle args, SuggestData result) {
Log.w(ex); data = result;
try {
// Fallback to UI thread (Android 5.1.1)
data = getSuggestData(getContext());
_suggest(args.getString("query")); _suggest(args.getString("query"));
} catch (Throwable exex) {
Log.unexpectedError(getParentFragmentManager(), exex);
} }
}
private SuggestData getSuggestData(Context context) {
SuggestData data = new SuggestData();
data.titles = new String[TAB_PAGES.length];
data.views = new View[TAB_PAGES.length];
LayoutInflater inflater = LayoutInflater.from(context); @Override
for (int tab = 0; tab < TAB_PAGES.length; tab++) { protected void onException(Bundle args, Throwable ex) {
data.titles[tab] = context.getString(PAGE_TITLES[tab]); Log.w(ex);
data.views[tab] = inflater.inflate(TAB_PAGES[tab], null); try {
// Fallback to UI thread (Android 5.1.1)
data = getSuggestData(getContext());
_suggest(args.getString("query"));
} catch (Throwable exex) {
Log.unexpectedError(getParentFragmentManager(), exex);
}
} }
return data; private SuggestData getSuggestData(Context context) {
} SuggestData data = new SuggestData();
}.serial().execute(FragmentOptions.this, args, "option:suggest"); data.titles = new String[TAB_PAGES.length];
} data.views = new View[TAB_PAGES.length];
private void _suggest(String query) { LayoutInflater inflater = LayoutInflater.from(context);
MatrixCursor cursor = new MatrixCursor(new String[]{"_id", "tab", "resid", "title"}); for (int tab = 0; tab < TAB_PAGES.length; tab++) {
data.titles[tab] = context.getString(PAGE_TITLES[tab]);
data.views[tab] = inflater.inflate(TAB_PAGES[tab], null);
}
if (data != null && return data;
query != null && query.length() > 1) { }
int id = 0; }.serial().execute(FragmentOptions.this, args, "option:suggest");
for (int tab = 0; tab < TAB_PAGES.length; tab++)
id = getSuggestions(query.toLowerCase(), id, tab, data.titles[tab], data.views[tab], cursor);
} }
searchView.setSuggestionsAdapter(new SimpleCursorAdapter( private void _suggest(String query) {
searchView.getContext(), MatrixCursor cursor = new MatrixCursor(new String[]{"_id", "tab", "resid", "title"});
R.layout.spinner_item1_dropdown,
cursor,
new String[]{"title"},
new int[]{android.R.id.text1},
0
));
searchView.getSuggestionsAdapter().notifyDataSetChanged();
}
private int getSuggestions(String query, int id, int tab, String title, View view, MatrixCursor cursor) { if (data != null &&
if (view == null || query != null && query.length() > 1) {
("nosuggest".equals(view.getTag()) && !BuildConfig.DEBUG)) int id = 0;
return id; for (int tab = 0; tab < TAB_PAGES.length; tab++)
else if (view instanceof ViewGroup) { id = getSuggestions(query.toLowerCase(), id, tab, data.titles[tab], data.views[tab], cursor);
ViewGroup group = (ViewGroup) view;
for (int i = 0; i <= group.getChildCount(); i++)
id = getSuggestions(query, id, tab, title, group.getChildAt(i), cursor);
} else if (view instanceof TextView) {
String description = ((TextView) view).getText().toString();
if (description.toLowerCase().contains(query)) {
description = description
.replace("%%", "%")
.replaceAll("%([0-9]\\$)?[sd]", "#");
String text = view.getContext().getString(R.string.title_title_description, title, description);
cursor.newRow()
.add(id++)
.add(tab)
.add(view.getId())
.add(text);
} }
searchView.setSuggestionsAdapter(new SimpleCursorAdapter(
searchView.getContext(),
R.layout.spinner_item1_dropdown,
cursor,
new String[]{"title"},
new int[]{android.R.id.text1},
0
));
searchView.getSuggestionsAdapter().notifyDataSetChanged();
} }
return id; private int getSuggestions(String query, int id, int tab, String title, View view, MatrixCursor cursor) {
} if (view == null ||
}); ("nosuggest".equals(view.getTag()) && !BuildConfig.DEBUG))
return id;
else if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0; i <= group.getChildCount(); i++)
id = getSuggestions(query, id, tab, title, group.getChildAt(i), cursor);
} else if (view instanceof TextView) {
String description = ((TextView) view).getText().toString();
if (description.toLowerCase().contains(query)) {
description = description
.replace("%%", "%")
.replaceAll("%([0-9]\\$)?[sd]", "#");
String text = view.getContext().getString(R.string.title_title_description, title, description);
cursor.newRow()
.add(id++)
.add(tab)
.add(view.getId())
.add(text);
}
}
return id;
}
});
if (!TextUtils.isEmpty(saved)) { if (!TextUtils.isEmpty(saved)) {
menuSearch.expandActionView(); menuSearch.expandActionView();
searchView.setQuery(saved, false); if (searchView != null)
searchView.setQuery(saved, false);
} }
getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleObserver() { getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleObserver() {

@ -349,25 +349,26 @@ public class FragmentRules extends FragmentBase {
} }
}); });
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { if (searchView != null)
@Override searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) { @Override
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { public boolean onQueryTextChange(String newText) {
searching = newText; if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
adapter.search(newText); searching = newText;
adapter.search(newText);
}
return true;
} }
return true;
}
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
searching = query; searching = query;
adapter.search(query); adapter.search(query);
}
return true;
} }
return true; });
}
});
MenuCompat.setGroupDividerEnabled(menu, true); MenuCompat.setGroupDividerEnabled(menu, true);

Loading…
Cancel
Save