我正在做这个页面上的"创建第二个活动"教程我每件事都做得很仔细,但还是出错了。我不明白为什么会出现错误,特别是当我只是复制/粘贴他们告诉我复制/粘贴的代码时!
下面的代码行:
ViewGroup layout = (VeiwGroup) findViewById(R.id.activity_display_message);
给了我这个错误:
嗯,我不知道那在说什么。我知道xml文件是一个相对布局。但我不知道为什么这是个问题,也不知道问题到底在哪里。
unexpected cast to ViewGroup: layout tag was RelativeLayout.
将ViewGroup
更改为RelativeLayout
当你调用ViewGroup类的时候你缺少了RelativeLayout方法的一些方法,因为ViewGroup是一个父类,而RelativeLayout只是它的高级版本和其他一些类。下面是这两个类的区别
@RemoteView
public class RelativeLayout extends ViewGroup {
public static final int ABOVE = 2;
public static final int ALIGN_BASELINE = 4;
public static final int ALIGN_BOTTOM = 8;
public static final int ALIGN_END = 19;
public static final int ALIGN_LEFT = 5;
public static final int ALIGN_PARENT_BOTTOM = 12;
public static final int ALIGN_PARENT_END = 21;
public static final int ALIGN_PARENT_LEFT = 9;
public static final int ALIGN_PARENT_RIGHT = 11;
public static final int ALIGN_PARENT_START = 20;
public static final int ALIGN_PARENT_TOP = 10;
public static final int ALIGN_RIGHT = 7;
public static final int ALIGN_START = 18;
public static final int ALIGN_TOP = 6;
public static final int BELOW = 3;
public static final int CENTER_HORIZONTAL = 14;
public static final int CENTER_IN_PARENT = 13;
public static final int CENTER_VERTICAL = 15;
public static final int END_OF = 17;
public static final int LEFT_OF = 0;
public static final int RIGHT_OF = 1;
public static final int START_OF = 16;
public static final int TRUE = -1;
public RelativeLayout(Context context) {
super((Context)null, (AttributeSet)null, 0, 0);
throw new RuntimeException("Stub!");
}
public RelativeLayout(Context context, AttributeSet attrs) {
super((Context)null, (AttributeSet)null, 0, 0);
throw new RuntimeException("Stub!");
}
public RelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super((Context)null, (AttributeSet)null, 0, 0);
throw new RuntimeException("Stub!");
}
public RelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super((Context)null, (AttributeSet)null, 0, 0);
throw new RuntimeException("Stub!");
}
public boolean shouldDelayChildPressedState() {
throw new RuntimeException("Stub!");
}
public void setIgnoreGravity(int viewId) {
throw new RuntimeException("Stub!");
}
public int getGravity() {
throw new RuntimeException("Stub!");
}
public void setGravity(int gravity) {
throw new RuntimeException("Stub!");
}
public void setHorizontalGravity(int horizontalGravity) {
throw new RuntimeException("Stub!");
}
public void setVerticalGravity(int verticalGravity) {
throw new RuntimeException("Stub!");
}
public int getBaseline() {
throw new RuntimeException("Stub!");
}
public void requestLayout() {
throw new RuntimeException("Stub!");
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
throw new RuntimeException("Stub!");
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
throw new RuntimeException("Stub!");
}
public RelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
throw new RuntimeException("Stub!");
}
protected android.view.ViewGroup.LayoutParams generateDefaultLayoutParams() {
throw new RuntimeException("Stub!");
}
protected boolean checkLayoutParams(android.view.ViewGroup.LayoutParams p) {
throw new RuntimeException("Stub!");
}
protected android.view.ViewGroup.LayoutParams generateLayoutParams(android.view.ViewGroup.LayoutParams lp) {
throw new RuntimeException("Stub!");
}
public CharSequence getAccessibilityClassName() {
throw new RuntimeException("Stub!");
}
public static class LayoutParams extends MarginLayoutParams {
@ExportedProperty(
category = "layout"
)
public boolean alignWithParent;
public LayoutParams(Context c, AttributeSet attrs) {
super((android.view.ViewGroup.LayoutParams)null);
throw new RuntimeException("Stub!");
}
public LayoutParams(int w, int h) {
super((android.view.ViewGroup.LayoutParams)null);
throw new RuntimeException("Stub!");
}
public LayoutParams(android.view.ViewGroup.LayoutParams source) {
super((android.view.ViewGroup.LayoutParams)null);
throw new RuntimeException("Stub!");
}
public LayoutParams(MarginLayoutParams source) {
super((android.view.ViewGroup.LayoutParams)null);
throw new RuntimeException("Stub!");
}
public LayoutParams(RelativeLayout.LayoutParams source) {
super((android.view.ViewGroup.LayoutParams)null);
throw new RuntimeException("Stub!");
}
public String debug(String output) {
throw new RuntimeException("Stub!");
}
public void addRule(int verb) {
throw new RuntimeException("Stub!");
}
public void addRule(int verb, int subject) {
throw new RuntimeException("Stub!");
}
public void removeRule(int verb) {
throw new RuntimeException("Stub!");
}
public int getRule(int verb) {
throw new RuntimeException("Stub!");
}
public int[] getRules() {
throw new RuntimeException("Stub!");
}
public void resolveLayoutDirection(int layoutDirection) {
throw new RuntimeException("Stub!");
}
}
}
ViewGroup类
你可以看到你没有调用相对布局所需的一些方法,这就是你得到错误的原因。
在文章(你给的链接)你可以找到注释:
注意:以前版本的Android Studio生成的XML布局可能不包含android:id属性。调用findViewById()如果布局没有android:id属性,将会失败。如果在这种情况下,打开activity_display_message.xml并添加属性android:id="@+id/activity_display_message"的布局
元素。