Android 无法从视图强制转换为复选框
我一直在尝试在eclipses workbench中构建我的应用程序,但是我偶然发现一条错误消息,上面写着“不能从视图转换为复选框”。我尝试了不同的方法来解决这个问题,但都没有结果,我想知道是否有人能帮我解决这个问题Android 无法从视图强制转换为复选框,android,Android,我一直在尝试在eclipses workbench中构建我的应用程序,但是我偶然发现一条错误消息,上面写着“不能从视图转换为复选框”。我尝试了不同的方法来解决这个问题,但都没有结果,我想知道是否有人能帮我解决这个问题 import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.view.View; import android.widge
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.widget.Checkbox;
import android.widget.TextView;
@SuppressLint("ParserError")
public class MainActivity extends Activity {
TextView textView;
CheckBox pepBox, cheeseBox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pepBox =
(CheckBox) findViewById(R.id.checkBox1);
cheeseBox =
(CheckBox) findViewById(R.id.checkBox2);
textView =
(TextView) findViewById(R.id.textView2);
}
这就是我使用的编码
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:text="@string/pepperoni" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/checkBox1"
android:layout_marginTop="33dp"
android:text="@string/extra_cheese" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/checkBox1"
android:layout_below="@+id/textView1"
android:onClick="onButton1Click"
android:text="@string/show" />
这是我的xml文件
谢谢让我有点挠头。我必须将您的代码加载到eclipse中才能找到它。更改
导入android.widget.Checkbox
到导入android.widget.CheckBox代码>。(注意复选框中的大写B。)当我进行更改时,您发布的代码工作正常
eclipse没有突出显示有问题的导入吗?将鼠标光标悬停在突出显示上会为您提供一些修复该突出显示的线索。(如果我听起来有点自命不凡,请原谅。很难说一个低代表性的人是该网站的新手还是该平台的新手)非常感谢,这解决了我的问题。奇怪的是,这么小的一个错误竟然让你想了这么久。不,那很好,你听起来一点也不傲慢,实际上很有帮助。是的,你是对的,eclipse通常会给你提示如何解决这个问题,我的大多数其他问题都是通过eclipse提示来解决的,但是这个错误并没有给我任何提示。感谢未来,这个错误将帮助我们找出问题所在。我经常不得不用谷歌搜索错误信息,以找到可能导致错误的线索。别忘了,你犯下一个以前没人见过的错误的可能性微乎其微。祝你好运。