问题
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