From 14162a478bb1ef4ea6df673a23f99652aac8af78 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Thu, 17 Mar 2016 18:43:20 +0100 Subject: [PATCH] fix several crashes --- .../habitrpg/android/habitica/APIHelper.java | 21 +++++-- .../habitica/ui/activities/MainActivity.java | 10 +++- .../ui/activities/TaskFormActivity.java | 3 +- .../social/PublicGuildsFragment.java | 4 +- .../ui/fragments/tasks/TasksFragment.java | 57 +++++++++++-------- 5 files changed, 59 insertions(+), 36 deletions(-) diff --git a/Habitica/src/com/habitrpg/android/habitica/APIHelper.java b/Habitica/src/com/habitrpg/android/habitica/APIHelper.java index 0a39214a9..9c0d954d8 100644 --- a/Habitica/src/com/habitrpg/android/habitica/APIHelper.java +++ b/Habitica/src/com/habitrpg/android/habitica/APIHelper.java @@ -260,13 +260,22 @@ public class APIHelper implements ErrorHandler, Profiler { ErrorResponse res = null; try { - res = (ErrorResponse) cause.getBodyAs(ErrorResponse.class) ; + res = new ErrorResponse(); + res.err = (String) cause.getBodyAs(String.class); } catch (RuntimeException e) { - //Can cause errors when error is a list and not a string - ErrorListResponse resList = (ErrorListResponse) cause.getBodyAs(ErrorListResponse.class); - if (resList.err != null && resList.err.size() >= 1) { - res = new ErrorResponse(); - res.err = resList.err.get(0); + try { + res = (ErrorResponse) cause.getBodyAs(ErrorResponse.class); + } catch (RuntimeException e2) { + try { + ErrorListResponse resList = (ErrorListResponse) cause.getBodyAs(ErrorListResponse.class); + if (resList.err != null && resList.err.size() >= 1) { + res = new ErrorResponse(); + res.err = resList.err.get(0); + } + } catch (RuntimeException e3) { + res = null; + } + } } diff --git a/Habitica/src/com/habitrpg/android/habitica/ui/activities/MainActivity.java b/Habitica/src/com/habitrpg/android/habitica/ui/activities/MainActivity.java index d6017b7a0..d34e814ac 100644 --- a/Habitica/src/com/habitrpg/android/habitica/ui/activities/MainActivity.java +++ b/Habitica/src/com/habitrpg/android/habitica/ui/activities/MainActivity.java @@ -498,7 +498,9 @@ public class MainActivity extends BaseActivity implements HabitRPGUserCallback.O drawer.getDrawerLayout().closeDrawer(GravityCompat.END); } else { super.onBackPressed(); - this.activeFragment.updateUserData(user); + if (this.activeFragment != null) { + this.activeFragment.updateUserData(user); + } } } @@ -902,6 +904,10 @@ public class MainActivity extends BaseActivity implements HabitRPGUserCallback.O } public String getUserID(){ - return user.getId(); + if (this.user != null) { + return user.getId(); + } else { + return ""; + } } } diff --git a/Habitica/src/com/habitrpg/android/habitica/ui/activities/TaskFormActivity.java b/Habitica/src/com/habitrpg/android/habitica/ui/activities/TaskFormActivity.java index e85c94624..154d2e15d 100644 --- a/Habitica/src/com/habitrpg/android/habitica/ui/activities/TaskFormActivity.java +++ b/Habitica/src/com/habitrpg/android/habitica/ui/activities/TaskFormActivity.java @@ -73,7 +73,6 @@ public class TaskFormActivity extends BaseActivity implements AdapterView.OnItem private List tags; private List tagsName; //added this private CheckListAdapter checklistAdapter; - private CheckBox tagsCheckBox; //added this private List userSelectedTags; private List allTags; private List userSelectedTagIds; @@ -465,7 +464,7 @@ public class TaskFormActivity extends BaseActivity implements AdapterView.OnItem for(int i = 0; i < tagsName.size(); i++){ TableRow row = new TableRow(tagsContainerLinearLayout.getContext()); row.setId(i); - tagsCheckBox = new CheckBox(tagsContainerLinearLayout.getContext()); + CheckBox tagsCheckBox = new CheckBox(tagsContainerLinearLayout.getContext()); tagsCheckBox.setText(tagsName.get(i)); // set text Name tagsCheckBox.setId(i); //This is to check if the tag was selected by the user. Similar to onClickListener diff --git a/Habitica/src/com/habitrpg/android/habitica/ui/fragments/social/PublicGuildsFragment.java b/Habitica/src/com/habitrpg/android/habitica/ui/fragments/social/PublicGuildsFragment.java index 7432a2925..28ba5ebf2 100644 --- a/Habitica/src/com/habitrpg/android/habitica/ui/fragments/social/PublicGuildsFragment.java +++ b/Habitica/src/com/habitrpg/android/habitica/ui/fragments/social/PublicGuildsFragment.java @@ -61,7 +61,9 @@ public class PublicGuildsFragment extends BaseMainFragment implements Callback ViewFragmentsDictionary = new HashMap<>(); private TagsHelper tagsHelper; // This will be used for this fragment. Currently being used to help filtering - private TagsHelper tagsNameHelper; // Added this so other activities/fragments can get the String names, not IDs - private TagsHelper tagsIdHelper; // Added this so other activities/fragments can get the IDs + private ArrayList tagNames; // Added this so other activities/fragments can get the String names, not IDs + private ArrayList tagIds; // Added this so other activities/fragments can get the IDs private ContentCache contentCache; private boolean displayingTaskForm; @@ -119,25 +119,6 @@ public class TasksFragment extends BaseMainFragment implements OnCheckedChangeLi this.tagsHelper = new TagsHelper(); } - //Without these following if statements, the Fragment has no way to pass the Tags to other activities/fragments - - if (this.tagsIdHelper == null && user != null) { - this.tagsIdHelper = new TagsHelper(); - //Pass in the information of the user because TagsHelper does the filtering. - for (Tag userTags : user.getTags()) { - tagsIdHelper.addTags(userTags.getId()); - } - } - - //This is to pass the names into other activities, not just their IDs - if (this.tagsNameHelper == null && user != null) { - this.tagsNameHelper = new TagsHelper(); - for (Tag userTags : user.getTags()) { - - tagsNameHelper.addTags(userTags.getName()); - } - } - if (user != null) { fillTagFilterDrawer(user.getTags()); } @@ -391,8 +372,8 @@ public class TasksFragment extends BaseMainFragment implements OnCheckedChangeLi } Bundle bundle = new Bundle(); bundle.putString("type", type); - bundle.putStringArrayList("tagsId", new ArrayList<>(this.tagsIdHelper.getTags())); - bundle.putStringArrayList("tagsName", new ArrayList<>(this.tagsNameHelper.getTags())); + bundle.putStringArrayList("tagsId", new ArrayList<>(this.getTagIds())); + bundle.putStringArrayList("tagsName", new ArrayList<>(this.getTagNames())); Intent intent = new Intent(activity, TaskFormActivity.class); intent.putExtras(bundle); @@ -437,8 +418,8 @@ public class TasksFragment extends BaseMainFragment implements OnCheckedChangeLi Bundle bundle = new Bundle(); bundle.putString("type", event.Task.getType()); bundle.putString("taskId", event.Task.getId()); - bundle.putStringArrayList("tagsId", new ArrayList<>(this.tagsIdHelper.getTags())); - bundle.putStringArrayList("tagsName", new ArrayList<>(this.tagsNameHelper.getTags())); + bundle.putStringArrayList("tagsId", new ArrayList<>(this.getTagIds())); + bundle.putStringArrayList("tagsName", new ArrayList<>(this.getTagNames())); Intent intent = new Intent(activity, TaskFormActivity.class); intent.putExtras(bundle); this.displayingTaskForm = true; @@ -556,4 +537,30 @@ public class TasksFragment extends BaseMainFragment implements OnCheckedChangeLi public String getDisplayedClassName() { return null; } + + private ArrayListgetTagNames() { + if (this.tagNames == null) { + this.tagNames = new ArrayList<>(); + } + if (this.user != null && this.user.getTags().size() != this.tagNames.size()) { + this.tagNames.clear(); + for (Tag tag : this.user.getTags()) { + this.tagNames.add(tag.getName()); + } + } + return this.tagNames; + } + + private ArrayListgetTagIds() { + if (this.tagIds == null) { + this.tagIds = new ArrayList<>(); + } + if (this.user != null && this.user.getTags().size() != this.tagIds.size()) { + this.tagIds.clear(); + for (Tag tag : this.user.getTags()) { + this.tagIds.add(tag.getName()); + } + } + return this.tagIds; + } }