2015-03-31 42 views
-1

我有一個有ListView的主視圖。Android誤解佈局文件

我修改了另一個活動的佈局,現在我的應用程序無法啓動。

錯誤:

java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ListView

我有這樣的XML:

<ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#0000" 
         android:id="@+id/list" > 

佈局相同文件的另一部分:

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/btnLogin" android:text="@string/okay"/> 

當我用這個,它顯示了以下:

View v= findViewById(R.id.list); 
((Button)v)).getText(); //value: "OK", but how? It is a list, not a button, yet it works 
ListView listView=(ListView) v; //this is where the error is 

R.java中我只能找到1個項目list。我已經清除了緩存。

所以問題是:列表中有列表的ID,但Android將它解析爲按鈕(它具有不同的ID)。

你能告訴我,發生了什麼事嗎?

+1

也許我失去了一些東西,但你爲什麼要在這裏'v''(查看)v)'當它已經是'View'?但是,這就是你的'ListView'的'id',那麼你試圖將它轉換爲同一行中的'Button'。這有點令人困惑,你期望在這裏或正在嘗試做什麼。 – codeMagic 2015-03-31 20:23:30

+0

那麼你爲什麼要從'R.id.list'''''''''''''''View''''按鈕',如果你想要的按鈕,你需要通過ID拉 – tyczj 2015-03-31 20:23:42

+0

你在哪一行獲得錯誤?另外請顯示完整的佈局,不只是片段,以便我們可以找出發生了什麼。 – dhke 2015-03-31 20:26:48

回答

0

我重新啓動了我的電腦,刪除了每個非必要的目錄,清除了緩存,向Bill Gates發送了一個快速禱告,現在它可以工作。偉大的建築:)