【发布时间】: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