-1

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);
}
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Jack Butler
  • 3
  • 1
  • 8

1 Answers1

0

Here:

postToolbar.inflateMenu(R.menu.menu_post);

You are inflating menu and you are given the Toolbar id-object as you can see. It should be your menu if you are inflating menu.

There is also an already initialized Toolbar (with the same id) in your RecyclerView.ViewHolder:

toolbar = view.findViewById(R.id.post_toolbar);

Anyways, why are you initializing Toolbar in PostViewHolder or onCreateViewHolder ? It shouldn't be there tho. You may wanna consider reading what actually Toolbar is.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • First off, I know what a toolbar is and how it works. Second, that code was there because I was testing something. Lastly, your suggestion doesn't work. – Jack Butler Aug 31 '18 at 19:02
  • No, I’m not talking about what is it, I’m talking about where to use it and that was interesting. Anyways, what happened when you used my suggestion? Any errors or? – ʍѳђઽ૯ท Aug 31 '18 at 19:04
  • I fixed the issue by fixing an XML reference to one of the text boxes in the post card view. – Jack Butler Sep 02 '18 at 22:46