2015-03-31 87 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发送了一个快速祷告,现在它可以工作。伟大的建筑:)