I am building a social media type app and for the main feed, I am using a CardView
with a Toolbar
inside to easily give me the ability to add an options menu for removing, editing, or reporting a post.
I have the function to launch a profile Activity
when the user's profile picture is clicked in the post. Once this is open, it displays some simple stats about the user, and shows you all of their posts.
The issue I am having, is that the PostViewHolder
class is throwing a NullPointerException
on the Toolbar
when the user's profile photo is clicked and the app is trying to launch their profile.
This whole process was working before I added the Toolbar
into the CardView
, I was just trying to clean up my approach on the posts and add a bit more:
me: FATAL EXCEPTION: main
Process: com.apexsoftware.quotable, PID: 6821
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.inflateMenu(int)' on a null object reference
at com.apexsoftware.quotable.adapter.holders.PostViewHolder.<init>(PostViewHolder.java:80)
at com.apexsoftware.quotable.adapter.PostsByUserAdapter.onCreateViewHolder(PostsByUserAdapter.java:41)
Here is my PostViewHolder
class:
public PostViewHolder(View view, final OnClickListener onClickListener, boolean isAuthorNeeded) {
super(view);
this.context = view.getContext();
likesCounterTextView = (TextView) view.findViewById(R.id.tv_like_count);
likesImageView = (ImageView) view.findViewById(R.id.iv_like);
dateTextView = (TextView) view.findViewById(R.id.tv_date);
userTextView = (TextView) view.findViewById(R.id.tv_user);
quoteTextView = (TextView) view.findViewById(R.id.tv_quote);
authorImageView = (ImageView) view.findViewById(R.id.iv_profile);
postToolbar = view.findViewById(R.id.post_toolbar);
authorImageView.setVisibility(isAuthorNeeded ? View.VISIBLE : View.GONE);
postToolbar.inflateMenu(R.menu.menu_post);
//profileManager = ProfileManager.getInstance(context.getApplicationContext());
postManager = PostManager.getInstance(context.getApplicationContext());
userManager = UserManager.getInstance(context.getApplicationContext());
/*view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (onClickListener != null && position != RecyclerView.NO_POSITION) {
onClickListener.onItemClick(getAdapterPosition(), v);
}
}
});*/
likesImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if (onClickListener != null && position != RecyclerView.NO_POSITION) {
onClickListener.onLikeClick(likeController, position);
}
}
});
authorImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (onClickListener != null && position != RecyclerView.NO_POSITION) {
onClickListener.onAuthorClick(getAdapterPosition(), v);
}
}
});
}
Here is my PostByUserAdapter
class:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.row_post, parent, false);
toolbar = view.findViewById(R.id.post_toolbar);
return new PostViewHolder(view, createOnClickListener(), false);
}