Error creating Poup menu in cardview

Asked

Viewed 51 times

1

I’m creating an application that I use cardview in my list. And I’m having difficulty adding the menu in cardview, as image below: inserir a descrição da imagem aqui

My class of cardview:

class CustomAdapter extends BaseAdapter{

    private List<Palheta> palhetas;

    public CustomAdapter(List<Palheta> palhetas) {
        this.palhetas = palhetas;
    }

    @Override
    public int getCount() {
        return palhetas.size();
    }

    @Override
    public Object getItem(int i) {
        return palhetas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        view = getLayoutInflater().inflate(R.layout.card_view_palheta, null);
        TextView tv_nome = (TextView) view.findViewById(R.id.tv_nome);
        simpleSwitch = (Switch) view.findViewById(R.id.simpleSwitch);
        ImageView imgCheck = view.findViewById(R.id.check);
        TextView textoCheck = 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;
    }
}

Mistake you’re making:

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

What am I missing?

  • 1

    The view imButton must be the type ImageView, otherwise you will not be able to convert from one type to another. Or you can replace ImageButton imageButton = (ImageButton) view.findViewById(R.id.imButton); for AppCompatButton imageButton = (AppCompatButton) view.findViewById(R.id.imButton);

  • Now I get the error: Process: com.diego.agentesendemias, PID: 12020 android.view.Inflateexception: Binary XML file line #0: Failed to resolve attribute at index 6: Typedvalue{t=0x1c/d=0xff009688 a=1 r=0x10600d3} at android.view.Layoutinflater.inflate(Layoutinflater.java:543) at android.view.Layoutinflater.inflate(Layoutinflater.java:427)

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);

On that line you’re trying to put the view imButton of the kind AppCompatButton in a kind ImageButton.

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

Browser other questions tagged

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