Problem with Recycleradpter Popupmenu

Asked

Viewed 81 times

0

I’m having trouble with Popupmenu, when I click on imageButtom fails and gives the following error

02-14 15:27:26.531 11084-11092/? E/art: Failed sending reply to Debugger: Broken pipe 02-14 15:27:38.842 11084-11084/com.robertoc.meublogclash E/Uncaughtexception: java.lang.Nullpointerexception: Attempt to invoke virtual method 'android.content.res.Context.getResources()' on a null Object Reference at android.support.v7.view.menu.MenuBuilder.(Menubuilder.java:223) at android.support.v7.widget.Popupmenu.(Popupmenu.java:103) at android.support.v7.widget.Popupmenu.(Popupmenu.java:78) at android.support.v7.widget.Popupmenu.(Popupmenu.java:63) Comments$Bloviewholderr$1.onClick(comments.java:190) at android.view.View.performClick(View.java:5217) at android.view.View$Performclick.run(View.java:21349) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.Activitythread.main(Activitythread.java:5585) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:730) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:620) 02-14 15:27:39.073 11084-11084/com.robertoc.meublogclash E/Androidruntime: FATAL EXCEPTION: main Process: com.robertoc.meublogclash, PID: 11084 java.lang.Nullpointerexception: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null Object Reference at android.support.v7.view.menu.MenuBuilder.(Menubuilder.java:223) at android.support.v7.widget.Popupmenu.(Popupmenu.java:103) at android.support.v7.widget.Popupmenu.(Popupmenu.java:78) at android.support.v7.widget.Popupmenu.(Popupmenu.java:63) Comments$Bloviewholderr$1.onClick(comments.java:190) at android.view.View.performClick(View.java:5217) at android.view.View$Performclick.run(View.java:21349) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.Activitythread.main(Activitythread.java:5585) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:730) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:620)

@Override protected void onStart() { super.onStart();

    FirebaseRecyclerAdapter<Cont, BloviewHolderr> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Cont, BloviewHolderr>(

            Cont.class,
            R.layout.coment_row,
            BloviewHolderr.class,
            mQueryCurrentUser

    ) {
        @Override
        protected void populateViewHolder(BloviewHolderr viewHolder, Cont model, int position) {
            final String user_key = getRef(position).getKey();
            //viewHolder.set_nome(model.getName());
            viewHolder.setComentario(model.getComentario());
            viewHolder.setUsername(model.getUsername());
            viewHolder.setFtperfil(getApplicationContext(), model.getFtperfil());

            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent singleBlogIntent = new Intent (Comentarios.this, Perfil.class);
                    singleBlogIntent.putExtra("blog_id", user_key);
                    startActivity(singleBlogIntent);

                }
            });


        }

          }
    });
    mComentList.setAdapter(firebaseRecyclerAdapter);

}

public static class BloviewHolderr extends  RecyclerView.ViewHolder{

    View mView;
    ImageButton mMenuPopup;
    Context mContext;
    public BloviewHolderr(View itemView) {
        super(itemView);

        mView = itemView;
        mMenuPopup = (ImageButton) mView.findViewById(R.id.menuComentario);

        mMenuPopup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popup = new PopupMenu(mContext, mMenuPopup);//Linha do erro

                //Inflando o popup usando o arquivo xml
                popup.getMenuInflater().inflate(R.menu.menu_comentarioo, popup.getMenu());

                //Resgata o item clicado e mostra em um Toast
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {

                        return true;
                    }
                });
                popup.show();
            }
        });
    }
    public void setComentario (String comentario) {
        TextView post_coment = (TextView) mView.findViewById(R.id.messageComent);
        post_coment.setText(comentario);
    }
    public void setUsername (String username){
        TextView post_nome = (TextView)mView.findViewById(R.id.nameComent);
        post_nome.setText(username);


    }

The error is in the following line, Popup menu popup = new Popup menu(mContext, mMenuPopup);

1 answer

2


The error occurs because you are not instantiating your mContext. You declare it but it remains null. An example of how it is:

mContext = null; //como você não instanciou ele está nulo
PopupMenu popup = new PopupMenu(mContext, mMenuPopup); // então dá erro aqui

A possible solution is you make the mContext receives the context of view. See how it should look:

mContext = mView.getContext();
PopupMenu popup = new PopupMenu(mContext, mMenuPopup);
  • 1

    Perfect 100% functional thank you very much helped me with the two answers

  • Hello Ack Lay, I came to work with this code today and I have a new problem if you can help me there http://answall.com/questions/186260/como-fa%C3%a7o-para-editar-uma-Child-no-firebase/186265? noredirect=1#comment385243_186265

  • @Wallaceroberto did not understand. What would be the problem?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.