Skip to content
Advertisement

android.support.v7.widget.AppCompatButton cannot be cast to android.widget.LinearLayout

I’m developing an app and I had some errors. When I run on the tablet it gives this error, but when I go to mobile it works perfectly. I already tried to clean and rebuild and tried to reboot the computer but I’m still having errors.

See the code below:

private LinearLayout btSobre, btCatalogo, btDistribuidores, btCadastro;

    btCatalogo = (LinearLayout) findViewById(R.id.btCatalogo);
    btDistribuidores = (LinearLayout) findViewById(R.id.btDistribuidores);
    btSobre = (LinearLayout) findViewById(R.id.btSobre);
    btCadastro = (LinearLayout) findViewById(R.id.btCadastro);

  btCadastro.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chamarCadastro();
        }
    });

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

            Intent chamada = new Intent(v.getContext(), SobreActivity.class);
            startActivity(chamada);

        }
    });


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

            Intent chamarCatalogo = new Intent(v.getContext(), CatalogoActivity.class);
            chamarCatalogo.putExtra("conexao", conexao);
            startActivity(chamarCatalogo);


        }
    });

    btDistribuidores.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent chamarDistribuidores = new Intent(v.getContext(), DistribuidorActivity.class); //antigo porem na terceira entrega

            startActivity(chamarDistribuidores);

        }
    });

XML

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="5dp"
            android:layout_weight="1"
            android:background="@drawable/quadrado_curto"
            android:orientation="vertical"
            android:clickable="true"
            android:id="@+id/btCatalogo">

And the others linearlayout is the same, the error that show me is

java.lang.RuntimeException: Unable to start activity ComponentInfo{....MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.LinearLayout

The strange thing is that the error only shows when I run in tablets, and on a regular phone it’s normal.

Advertisement

Answer

the strange is that the error only show when i run in tablets, in regular phone its normal

Do you have any additional layout- folders declared in /res? My guess would be that you have another layout file sharing the same name in a size-specific folder (e.g. in layout-xlarge) which has android:id="@+id/btCatalogo" assigned to an AppCompatButton rather than a LinearLayout.

See also – Supporting Multiple Screens

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement