2012-04-28 82 views
0

我修改別人的代碼,只是想第四複選框添加到下面的對話框:爲什麼添加複選框到XML佈局會導致java.lang.ClassCastException?

<?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); 

和這裏的主.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> 
+2

什麼是SelectBookActivity.java:586包含 – 2012-04-28 04:50:11

+1

編輯後的代碼SelectBookActivity.class – 2012-04-28 04:52:45

+0

謝謝,我已經添加了它,但我沒有看到它是如何相關的......然後,我仍然在學習Java ... – yuttadhammo 2012-04-28 05:00:29

回答

1

textInfo = (TextView)your_layout.findViewById(R.id.text_info); 

代替

textInfo = (TextView)findViewById(R.id.text_info); 
+0

謝謝,但我敢肯定,這不是問題... text_info是在主佈局中,但複選框是在啓動,afaics不會調用的對話框中。再次,這只是添加第四個複選框到未調用的對話框的問題。 – yuttadhammo 2012-04-28 05:07:02

+1

@Yuttadhammo:我一步一步檢查你的代碼,因爲你在這段代碼中共享你沒有問題可以發佈其他部分的代碼和完整的XML然後我們會發現確切的問題 – 2012-04-28 05:09:10

+0

謝謝;我應該添加哪部分代碼?這是一個非常大的項目。整個項目在github上:https://github.com/ssutee/E-Tipitaka-for-Android,但我做了一些修改,所以它們也可能是問題所在。 – yuttadhammo 2012-04-28 05:13:50

0

確保當您使用

myContext.findViewById(R.id.cb_etc); 

將其分配給CheckBox對象。

CheckBox mCheckBox = (CheckBox) myContext.findViewById(R.id.cb_etc); 

注意:如果您使用的是其他佈局,則需要使用該佈局或佈局的上下文來投射findViewBy標識。

+0

謝謝,我這樣做是:\t \t \t \t \t布爾B4 =((複選框)cateDialog.findViewById(R.id.cb_etc))器isChecked();但是這個代碼並沒有在啓動時運行,它只是一個搜索對話框,它是在點擊按鈕時觸發的。 – yuttadhammo 2012-04-28 05:03:47

0

我也有同樣的問題,如果text_info是在不同勢佈局,然後使用。但我在Linearlayout中添加了一個屬性id

 <LinearLayout 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/mainlayout" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 

解決了。

+0

謝謝,這實際上幫助...但現在它引發下一行的錯誤:readBtn =(Button)findViewById(R.id.read_btn );因爲read_btn也位於main.xml佈局中,所以我對於問題是什麼感到困惑。 – yuttadhammo 2012-04-28 05:39:44

+0

可以在這裏發佈main.xml的代碼嗎?因爲需要查看read_btn的代碼。 – AndroGeek 2012-04-28 05:47:13

+0

main.xml添加... – yuttadhammo 2012-04-28 12:10:20

0

實際上,答案竟然是運行ant clean - 問題是gen目錄中有緩存文件導致設備混亂。

相關問題