【问题标题】:android.support.v7.widget.AppCompatButton cannot be cast to android.widget.LinearLayoutandroid.support.v7.widget.AppCompatButton 无法转换为 android.widget.LinearLayout
【发布时间】:2017-01-12 19:17:20
【问题描述】:

我正在开发一个应用程序,但出现了一些错误。当我在平板电脑上运行时,它会出现此错误,但是当我使用移动设备时,它运行良好。我已经尝试清理和重建并尝试重新启动计算机,但我仍然遇到错误。

请看下面的代码:

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">

和其他linearlayout一样,给我看的错误是

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

奇怪的是,这个错误只在我在平板电脑上运行时出现,在普通手机上是正常的。

【问题讨论】:

  • 也许您应该提供MainActivity 并显示发生此错误的行?我看不出当前显示的代码抛出此类错误的原因
  • 发生此错误时的行是我尝试投射线性布局时:btCatalogo = (LinearLayout) findViewById(R.id.btCatalogo);
  • 您是在任何方法中初始化视图还是在类中初始化视图?

标签: java android android-layout


【解决方案1】:

奇怪的是,这个错误只在我在平板电脑上运行时出现,在普通手机上是正常的

您是否在/res 中声明了任何其他layout- 文件夹?我的猜测是,您在特定大小的文件夹(例如 layout-xlarge)中有另一个布局文件共享相同的名称,其中 android:id="@+id/btCatalogo" 分配给 AppCompatButton 而不是 LinearLayout

另请参阅 - Supporting Multiple Screens

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2020-08-22
    • 2014-01-11
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    相关资源
    最近更新 更多