意外转换为ViewGroup:布局标记为RelativeLayout

时间:2016-10-30 07:29:43

标签: android android-layout android-view

我在这个page上做“创建第二个活动”教程 我已经仔细地做了一切,但是我收到了一个错误。我不明白为什么会出现错误,特别是因为我只是复制/粘贴他们告诉我复制/粘贴的代码!

以下代码行:

ViewGroup layout = (VeiwGroup)  findViewById(R.id.activity_display_message);

给了我这个错误:

  

unexpected cast to ViewGroup: layout tag was RelativeLayout.

好吧,我不知道那是怎么回事。我知道xml文件是相对布局。但我不知道为什么这是一个问题或问题确实存在。

2 个答案:

答案 0 :(得分:0)

将您的ViewGroup更改为RelativeLayout

当您调用ViewGroup类时,您缺少RelativeLayout方法的一些方法,因为ViewGroup是父类,而相对布局只是它的高级版本,而其他类是这两个类之间的区别

@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 class

您可以看到您没有调用relativelayout所需的某些方法。这就是您遇到错误的原因。

答案 1 :(得分:-1)

在文章(您给出的链接)中,您可以找到注释:

  

注意:以前版本的Android Studio生成的XML布局   可能不包含android:id属性。调用findViewById()   如果布局没有android:id属性,则会失败。如果   就是这种情况,打开activity_display_message.xml并添加   属性android:id =“@ + id / activity_display_message”到布局   元件。

添加缺失的属性,错误应该消失。