Why does adding a checkbox to an XML layout cause a java.lang.ClassCastException?

kakahelmy

New Member
I am modifying someone else's code, and just trying to add a fourth checkbox to the following dialog:\[code\]<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"><CheckBox android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_width="wrap_content" android:text="@string/vinai_full" android:checked="true" android:id="@+id/cb_vinai" android:layout_height="wrap_content"/><CheckBox android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_width="wrap_content" android:text="@string/suttan_full" android:checked="true" android:id="@+id/cb_suttan" android:layout_height="wrap_content"/><CheckBox android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_width="wrap_content" android:text="@string/abhidum_full" android:checked="true" android:id="@+id/cb_abhidham" android:layout_height="wrap_content"/><Button android:text="@string/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/okcatebtn" android:layout_marginTop="20dp" android:layout_marginBottom="10dp" android:layout_gravity="center" /></LinearLayout>\[/code\]The fourth checkbox is:\[code\]<CheckBox android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_width="wrap_content" android:text="@string/etc_full" android:checked="true" android:id="@+id/cb_etc" android:layout_height="wrap_content"/>\[/code\]When I add it in, the apk compiles fine, but the program crashes on startup:\[code\]E/AndroidRuntime( 1470): FATAL EXCEPTION: mainE/AndroidRuntime( 1470): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.yuttadhammo.tipitaka/org.yuttadhammo.tipitaka.SelectBookActivity}: java.lang.ClassCastException: android.widget.GalleryE/AndroidRuntime( 1470): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)E/AndroidRuntime( 1470): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)E/AndroidRuntime( 1470): at android.app.ActivityThread.access$1500(ActivityThread.java:117)E/AndroidRuntime( 1470): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)E/AndroidRuntime( 1470): at android.os.Handler.dispatchMessage(Handler.java:99)E/AndroidRuntime( 1470): at android.os.Looper.loop(Looper.java:123)E/AndroidRuntime( 1470): at android.app.ActivityThread.main(ActivityThread.java:3683)E/AndroidRuntime( 1470): at java.lang.reflect.Method.invokeNative(Native Method)E/AndroidRuntime( 1470): at java.lang.reflect.Method.invoke(Method.java:507)E/AndroidRuntime( 1470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)E/AndroidRuntime( 1470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)E/AndroidRuntime( 1470): at dalvik.system.NativeStart.main(Native Method)E/AndroidRuntime( 1470): Caused by: java.lang.ClassCastException: android.widget.GalleryE/AndroidRuntime( 1470): at org.yuttadhammo.tipitaka.SelectBookActivity.onCreate(SelectBookActivity.java:586)E/AndroidRuntime( 1470): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)E/AndroidRuntime( 1470): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)E/AndroidRuntime( 1470): ... 11 more\[/code\]This is even though the dialog is not loaded at startup. If I replace the third checkbox with the fourth one, there is no crash, so it seems like for some reason it is simply not accepting a fourth element. Why would that be?EDIT: Here's the code leading up to the error, but I don't understand how it is related, since it refers to an element in a differing layout file:\[code\]@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); main = View.inflate(this, R.layout.main, null); setContentView(main); searchHistoryDBAdapter = new SearchHistoryDBAdapter(SelectBookActivity.this); searchResultsDBAdapter = new SearchResultsDBAdapter(SelectBookActivity.this); bookmarkDBAdapter = new BookmarkDBAdapter(SelectBookActivity.this); Context context = getApplicationContext(); prefs = PreferenceManager.getDefaultSharedPreferences(context); MainTipitakaDBAdapter mainTipitakaDBAdapter = new MainTipitakaDBAdapter(this); try { mainTipitakaDBAdapter.open(); if(mainTipitakaDBAdapter.isOpened()) { mainTipitakaDBAdapter.close(); } else { startDownloader(); } } catch (SQLiteException e) { Log.e ("Tipitaka","error:", e); startDownloader(); } Resources res = getResources(); final String [] cnames = res.getStringArray(R.array.category); textInfo = (TextView) findViewById(R.id.text_info);\[/code\]and here's main.xml:\[code\]<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollView01" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="horizontal|vertical"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_gravity="center_horizontal" android:padding="3dp" android:id="@+id/about_header"> <TextView android:id="@+id/about_text_1" android:layout_marginLeft="1dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/about_logo" android:layout_centerVertical="true" android:text="Android Tipitaka" android:textSize="24sp" android:textColor="@android:color/primary_text_dark_nodisable"/> </RelativeLayout> <View android:background="@drawable/black_white_gradient" android:layout_width="match_parent" android:layout_marginTop="10dp" android:layout_marginBottom="5dp" android:layout_height="2dp"/> <Gallery android:layout_width="match_parent" android:id="@+id/gallery_cate" android:spacing="6dp" android:gravity="center_vertical" android:layout_height="60dp"/> <TextView android:id="@+id/book_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/th_book_label" android:textSize="17sp" /> <Gallery android:id="@+id/gallery_ncate" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center_vertical" android:spacing="25dp" /> <TextView android:id="@+id/text_info" android:layout_width="match_parent" android:layout_height="match_parent" android:lines="2" android:layout_marginBottom="5dp" android:layout_gravity="center_vertical|center_horizontal|center" android:gravity="center_vertical|center_horizontal" android:text="" android:textSize="17sp" /> <RelativeLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/main_buttons" android:layout_gravity="center_horizontal"> <Button android:id="@+id/read_btn" android:layout_width="85dp" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:layout_gravity="center_vertical|center_horizontal|center" android:text="@string/th_read" android:textStyle="bold" android:textSize="16dp" android:layout_marginRight="40dp" /> <Button android:id="@+id/search_btn" android:text="@string/th_search" android:layout_toRightOf="@id/read_btn" android:layout_width="85dp" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:layout_gravity="center_vertical|center_horizontal|center" android:textStyle="bold" android:textSize="16dp" /> </RelativeLayout></LinearLayout></ScrollView>\[/code\]
 
Back
Top