【发布时间】:2018-01-13 11:07:00
【问题描述】:
【问题讨论】:
-
在这里发布代码和错误的图像是不受欢迎的,因为这会使我们更难帮助您解决问题。最好将相关代码和/或错误直接复制并粘贴到您的问题中。请阅读Why not to upload images of code on SO when asking a question?,然后阅读edit您的问题。
标签: java android android-layout
【问题讨论】:
标签: java android android-layout
在 xml 文件中检查您的relativelayout id。似乎 id rootlayout 属于 constraint layout。更改 xml 文件中相同的相对布局的 id。
或将您的 rootlayout 声明更改为
ContraintLayout rootlayout = (ConstraintLayout) findViewById(R.id.rootlayout);
【讨论】:
您不能转换对象,因为它是不同的类型,例如使用以下代码:
public class Fruit {
protected int size;
public Fruit(int size) {
this.size = size;
}
}
public class Banana extends Fruit {
private Color color;
public Banana(int size, Color color) {
super(size);
this.color = color;
}
}
public class Main {
public static void main(String[] args) {
Fruit someFruit = new Fruit(5);
Banana yellowNanner = new Banana(3, Color.YELLOW);
Fruit greenBanana = new Banana(4, Color.GREEN);
// Allowed cast because Banana is a fruit (extends it, no need to explicitly cast) [1]
Fruit generalBanana = yellowNanner;
// Allowed cast because The fruit was a banana originally [2]
Banana stillABanana = (Banana) greenBanana;
// Disallowed cast because Fruit isn't necessarily a banana [3]
Banana notABanana = (Banana) someFruit; // gives a runtime exception (ClassCastException)
// Disallowed cast, String isn't related to fruit [4]
Fruit definitelynotfruit = (Fruit) "Pretend to be fruit"; // gives a runtime exception, and probably a compiler one too
}
}
我想说的是,你正在施放一些无法施放的东西。检查您尝试转换为的东西实际上是超类还是与它来自的类相同的级别。
您遇到的情况可能是 [3] 或 [4]
但从以下方面判断:https://developer.android.com/reference/android/view/View.html 和:https://developer.android.com/reference/android/widget/RelativeLayout.html
RelativeLayout 是 View 的子类。 android.support.constraint.Constraintlayout 是 View 的子类
but a android.support.constraint.Constraintlayout can't be cast to a RelativeLayout
你基本上想要做的是
RelativeLayout relativeLayout = (RelativeLayout) constraintLayout;
RelativeLayout 不是 constraintLayout 的超类。
【讨论】: