Error creating sparrow up menu in cardview

1

I'm creating an application that uses cardview in my list. And I'm having trouble adding the menu in the cardview, as shown below:

Mycardviewclass:

classCustomAdapterextendsBaseAdapter{privateList<Palheta>palhetas;publicCustomAdapter(List<Palheta>palhetas){this.palhetas=palhetas;}@OverridepublicintgetCount(){returnpalhetas.size();}@OverridepublicObjectgetItem(inti){returnpalhetas.get(i);}@OverridepubliclonggetItemId(inti){returni;}@OverridepublicViewgetView(finalinti,Viewview,ViewGroupviewGroup){view=getLayoutInflater().inflate(R.layout.card_view_palheta,null);TextViewtv_nome=(TextView)view.findViewById(R.id.tv_nome);simpleSwitch=(Switch)view.findViewById(R.id.simpleSwitch);ImageViewimgCheck=view.findViewById(R.id.check);TextViewtextoCheck=view.findViewById(R.id.nomeCheck);if(palhetas.get(i).getSituacao().equals("COLETADA")){
           simpleSwitch.setVisibility(View.INVISIBLE);
           simpleSwitch.setEnabled(false );
           imgCheck.setVisibility(View.VISIBLE );
           textoCheck.setVisibility(View.VISIBLE);
        }
        ImageButton imageButton = (ImageButton) view.findViewById(R.id.imButton);
        try {
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu popup = new PopupMenu(getApplicationContext(), view);
                popup.getMenuInflater().inflate(R.menu.main_palheta, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(getApplicationContext(), "Você clicou em : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });
                popup.show();
            }
        });
        } catch (Exception e) {

            e.printStackTrace();
        }

        tv_nome.setText(palhetas.get(i).getCodigo());
        TextView tv_endereco = (TextView) view.findViewById(R.id.tv_endereco);
        tv_endereco.setText("Endereço: " + palhetas.get(i).getEndereco().getRua());
        TextView tv_bairro = (TextView) view.findViewById(R.id.tv_bairro);
        tv_bairro.setText("Bairro: " + palhetas.get(i).getEndereco().getBairro());
        idPalheta = palhetas.get(i).getId();

        simpleSwitch.setTextOn("Sim"); // displayed text of the Switch whenever it is in checked or on state
        simpleSwitch.setTextOff("Não");
        simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if (isChecked) {
                    alertDialog();
                } else {
                    Toast.makeText( getApplicationContext(), "NAO", Toast.LENGTH_LONG ).show();
                }
            }
        });
        return view;
    }
}

Error giving:

java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.ImageButton

What am I missing?

    
asked by anonymous 12.01.2018 / 21:27

1 answer

2

This exception is thrown when the type you declared in the class is different from the view you used in the layout.

ImageButton imageButton = (ImageButton) view.findViewById(R.id.imButton);

In this line you are trying to put the view imButton of type AppCompatButton into a type ImageButton .

The ideal here is to use AppCompatImageButton , both in the layout and in the Java class.

    
19.01.2018 / 14:15