Android - RelativeLayout inflated as Button?

我怕爱的太早我们不能终老 提交于 2019-12-12 01:29:09

问题


I have a weird problem. While running my Android application, I receive Exception:

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

the code, where I try to get relativeLayout

v = inflater.inflate(R.layout.home, null);
RelativeLayout btn = (RelativeLayout) v.findViewById(R.id.my_button);

in xml, I've got this:

<RelativeLayout                
                android:id="@+id/my_button" 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/button_selector"/>

While debugging, I've noticed that in v there is Button with mID equals to R.id.my_button.

Although, when I change id in xml and code to anything else, I receive NullPointerException.

Any ideas why this is happen ?

EDIT: Maybe it will be helpful, if I add that in older version of that file there was Button, but was replaced with RelativeLayout. I'm using GIT. Project was cleaned many times and eclipse was restarted also.


回答1:


Delete the R.Java file and once recreated run the app it will work fine.




回答2:


The problem was that I've made one more layout file that I forgot about and it was for the same Activity byt different configuration. Problem solved.



来源:https://stackoverflow.com/questions/9420187/android-relativelayout-inflated-as-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!