From b6dbdb7e6d3fbd560e0c5a3cd1ec7c3db0364820 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 3 Aug 2020 20:11:04 +0300 Subject: [PATCH] UnreadFragment crash fix --- .../activities/main/unread/UnreadFragment.java | 13 +++++++------ .../profile/latestPosts/LatestPostsAdapter.java | 16 ++++++++-------- .../profile/latestPosts/LatestPostsFragment.java | 3 ++- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java index 661ed1be..50b2b71d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.List; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.InvalidSessionException; @@ -247,11 +248,11 @@ public class UnreadFragment extends BaseFragment { else{ hideProgressUI(); if (resultCode == NetworkResultCodes.NETWORK_ERROR) - Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show(); else if (resultCode == SessionManager.INVALID_SESSION) - Toast.makeText(getContext(), "Session verification failed. Please try logging in again.", Toast.LENGTH_LONG).show(); + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Session verification failed. Please try logging in again.", Toast.LENGTH_LONG).show(); else - Toast.makeText(getContext(), "Unexpected error," + + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Unexpected error," + " please contact the developers with the details", Toast.LENGTH_LONG).show(); } } @@ -322,11 +323,11 @@ public class UnreadFragment extends BaseFragment { else{ hideProgressUI(); if (resultCode == NetworkResultCodes.NETWORK_ERROR) - Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show(); else if (resultCode == SessionManager.INVALID_SESSION) - Toast.makeText(getContext(), "Session verification failed. Please try logging out and back in again", Toast.LENGTH_LONG).show(); + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Session verification failed. Please try logging out and back in again", Toast.LENGTH_LONG).show(); else - Toast.makeText(getContext(), "Unexpected error," + + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Unexpected error," + " please contact the developers with the details", Toast.LENGTH_LONG).show(); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java index 2b5febed..09c3c1e0 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsAdapter.java @@ -9,6 +9,7 @@ import android.view.ViewGroup; import android.widget.RelativeLayout; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; @@ -49,13 +50,9 @@ class LatestPostsAdapter extends RecyclerView.Adapter { return parsedTopicSummaries.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM; } + @NonNull @Override - public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - if (viewType == VIEW_TYPE_EMPTY) { - View view = LayoutInflater.from(parent.getContext()). - inflate(R.layout.fragment_profile_latest_posts_empty_message, parent, false); - return new RecyclerView.ViewHolder(view){}; - } + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if (viewType == VIEW_TYPE_ITEM) { View view = LayoutInflater.from(parent.getContext()). inflate(R.layout.fragment_profile_latest_posts_row, parent, false); @@ -64,13 +61,16 @@ class LatestPostsAdapter extends RecyclerView.Adapter { View view = LayoutInflater.from(parent.getContext()). inflate(R.layout.recycler_loading_item, parent, false); return new LoadingViewHolder(view); + } else { // viewType == VIEW_TYPE_EMPTY + View view = LayoutInflater.from(parent.getContext()). + inflate(R.layout.fragment_profile_latest_posts_empty_message, parent, false); + return new RecyclerView.ViewHolder(view){}; } - return null; } @SuppressLint("ClickableViewAccessibility") @Override - public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { + public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) { if (holder instanceof LatestPostViewHolder) { PostSummary topic = parsedTopicSummaries.get(position); final LatestPostViewHolder latestPostViewHolder = (LatestPostViewHolder) holder; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java index 35f66a87..dd00d425 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java @@ -7,6 +7,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; +import androidx.annotation.NonNull; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -97,7 +98,7 @@ public class LatestPostsFragment extends BaseFragment implements LatestPostsAdap //latestPostsAdapter.setOnLoadMoreListener(); mainContent.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); totalItemCount = layoutManager.getItemCount(); lastVisibleItem = layoutManager.findLastVisibleItemPosition();