diff --git a/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java b/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java index 8f83a49eb..f643595a0 100644 --- a/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java +++ b/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java @@ -133,34 +133,39 @@ public class CategorizationFragment extends SherlockFragment{ protected void setCatsAfterAsync(ArrayList categories, String filter) { - ArrayList items = new ArrayList(); - HashSet existingKeys = new HashSet(); - for(CategoryItem item : categoriesAdapter.getItems()) { - if(item.selected) { - items.add(item); - existingKeys.add(item.name); + if (getActivity() != null) { + ArrayList items = new ArrayList(); + HashSet existingKeys = new HashSet(); + for (CategoryItem item : categoriesAdapter.getItems()) { + if (item.selected) { + items.add(item); + existingKeys.add(item.name); + } } - } - for(String category : categories) { - if(!existingKeys.contains(category)) { - items.add(new CategoryItem(category, false)); + for (String category : categories) { + if (!existingKeys.contains(category)) { + items.add(new CategoryItem(category, false)); + } } - } - categoriesAdapter.setItems(items); - categoriesAdapter.notifyDataSetInvalidated(); - categoriesSearchInProgress.setVisibility(View.GONE); + categoriesAdapter.setItems(items); + categoriesAdapter.notifyDataSetInvalidated(); + categoriesSearchInProgress.setVisibility(View.GONE); - if (categories.isEmpty()) { - if(TextUtils.isEmpty(filter)) { - // If we found no recent cats, show the skip message! - categoriesSkip.setVisibility(View.VISIBLE); + if (categories.isEmpty()) { + if (TextUtils.isEmpty(filter)) { + // If we found no recent cats, show the skip message! + categoriesSkip.setVisibility(View.VISIBLE); + } else { + categoriesNotFoundView.setText(getString(R.string.categories_not_found, filter)); + categoriesNotFoundView.setVisibility(View.VISIBLE); + } } else { - categoriesNotFoundView.setText(getString(R.string.categories_not_found, filter)); - categoriesNotFoundView.setVisibility(View.VISIBLE); + categoriesList.smoothScrollToPosition(existingKeys.size()); } - } else { - categoriesList.smoothScrollToPosition(existingKeys.size()); + } + else { + Log.e(TAG, "Error: Fragment is null"); } }