我正在修改其他人的代码,并试图在以下对话框中添加第四个复选框:
<?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>
第四个复选框是:
<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"/>
当我添加它时,apk 编译正常,但程序在启动时崩溃:
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
即使在启动时未加载对话框也是如此。如果我用第四个复选框替换第三个复选框,则不会发生崩溃,因此似乎出于某种原因它根本不接受第四个元素。为什么会这样?
编辑:这是导致错误的代码,但我不明白它是如何相关的,因为它引用了不同布局文件中的元素:
@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);
这是main.xml:
<?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>