【问题标题】:unexpected cast to ViewGroup: layout tag was RelativeLayout意外转换为 ViewGroup:布局标签是 RelativeLayout
【发布时间】:2017-02-16 15:13:45
【问题描述】:

我正在这个 page 上做“创建第二个活动”教程 我已经小心翼翼地完成了所有事情,但我遇到了一个错误。我不明白为什么会出现错误,尤其是当我只是复制/粘贴他们告诉我复制/粘贴的代码时!

以下代码行:

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

给我这个错误:

unexpected cast to ViewGroup: layout tag was RelativeLayout.

嗯,我不知道那是在说什么。我知道 xml 文件是一个相对布局。但我不知道为什么这是个问题,也不知道问题到底出在哪里。

【问题讨论】:

  • 您还需要发布相关代码。 activity_display_message 是 relativelayout 的 id!。
  • 我们需要查看您的 XML。我按照教程了解如何理解它的要求。无法复制

标签: android android-layout android-view


【解决方案1】:

将您的 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 所需的某些方法。这就是您遇到错误的原因。

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 虽然您可能是正确的,但如果这是真的,它会将 layout 分配为 null。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多