Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does adding a checkbox to an XML layout cause a java.lang.ClassCastException?
    primarykey
    data
    text
    <p>I am modifying someone else's code, and just trying to add a fourth checkbox to the following dialog:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"&gt; &lt;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"/&gt; &lt;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"/&gt; &lt;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"/&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>The fourth checkbox is:</p> <pre><code>&lt;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"/&gt; </code></pre> <p>When I add it in, the apk compiles fine, but the program crashes on startup:</p> <pre><code>E/AndroidRuntime( 1470): FATAL EXCEPTION: main E/AndroidRuntime( 1470): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.yuttadhammo.tipitaka/org.yuttadhammo.tipitaka.SelectBookActivity}: java.lang.ClassCastException: android.widget.Gallery E/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.Gallery E/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></pre> <p>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?</p> <p><em>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:</em></p> <pre><code>@Override public 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></pre> <p>and here's main.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"&gt; &lt;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"&gt; &lt;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"/&gt; &lt;/RelativeLayout&gt; &lt;View android:background="@drawable/black_white_gradient" android:layout_width="match_parent" android:layout_marginTop="10dp" android:layout_marginBottom="5dp" android:layout_height="2dp"/&gt; &lt;Gallery android:layout_width="match_parent" android:id="@+id/gallery_cate" android:spacing="6dp" android:gravity="center_vertical" android:layout_height="60dp"/&gt; &lt;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" /&gt; &lt;Gallery android:id="@+id/gallery_ncate" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center_vertical" android:spacing="25dp" /&gt; &lt;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" /&gt; &lt;RelativeLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/main_buttons" android:layout_gravity="center_horizontal"&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; &lt;/ScrollView&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload