ive been trying to build my app in eclipses workbench, however i have stumbled across an error message reading “cannot cast from view to checkbox”. I have tried different things to resolve the issue to no avail, was wondering if someone could please help me out
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);
}
that is the coding i have used
<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" />
that is my xml file
thanks
That had me scratching my head for a bit. I had to load your code into eclipse to find it. Change
import android.widget.Checkbox;toimport android.widget.CheckBox;. (Note the capital B in checkbox.) Your code as posted works fine when I make that change.Did eclipse not highlight the offending import? Hovering the mouse cursor over a highlight will give you some clues how to fix it. (forgive me if I sound patronizing. Its hard to tell if someone with a low rep is new to the website or new to the platform)