2

我一直在尝试在 Eclipse 工作台中构建我的应用程序,但是我偶然发现了一条错误消息,上面写着“无法从视图投射到复选框”。我尝试了不同的方法来解决问题无济于事,想知道是否有人可以帮助我

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文件谢谢

4

1 回答 1

5

这让我有点摸不着头脑。我必须将您的代码加载到 eclipse 中才能找到它。更改import android.widget.Checkbox;import android.widget.CheckBox;。(注意复选框中的大写 B。)当我进行更改时,您发布的代码可以正常工作。

eclipse 没有突出显示有问题的导入吗?将鼠标光标悬停在突出显示上将为您提供一些如何修复它的线索。(如果我听起来很傲慢,请原谅我。很难判断代表低的人是网站新手还是平台新手)

于 2012-07-14T04:23:54.963 回答