Android 无法使用var设置Kotlin中对象的属性
我正在尝试设置我在Android Studio中使用Kotlin创建的对象的属性。我使用for循环每次生成一个新对象并将其添加到数组中。当我初始化对象并尝试设置topId时,它会显示“Val无法重新分配”,即使我将其声明为varAndroid 无法使用var设置Kotlin中对象的属性,android,kotlin,Android,Kotlin,我正在尝试设置我在Android Studio中使用Kotlin创建的对象的属性。我使用for循环每次生成一个新对象并将其添加到数组中。当我初始化对象并尝试设置topId时,它会显示“Val无法重新分配”,即使我将其声明为var for (i in 1..5) { var topRanNum = generateRandomNum(topSize) var top = currentSeasonTops[topRanNum]
for (i in 1..5) {
var topRanNum = generateRandomNum(topSize)
var top = currentSeasonTops[topRanNum]
var topLoopCounter = 0
var topId = top.id
var newOutfit: Outfit = Outfit()
if(top.wornCount < 5 ) {
newOutfit.topId = top.id
}
}
发生这种情况是因为您需要在Java中为
topId
声明一个可从外部访问的setter,或者使变量可以从外部访问
例如
public void setTopId(长topId){
this.topId=topId;
}
请在这里发布装备
你好,Giorgio,我添加了装备类,你得到了什么错误?当我初始化我的对象并尝试设置topId时,它会说“Val无法重新分配”,即使我声明它为var.omg lol,谢谢,我已经看了太久了!我会接受这个答案。多谢各位much@olivia.dayaram没问题!
public Outfit() {}
public Outfit(Long topId, Long bottomId, String topPhotoPath, String bottomPhotoPath) {
this.topId = topId;
this.bottomId = bottomId;
this.topPhotoPath = topPhotoPath;
this.bottomPhotoPath = bottomPhotoPath;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTopPhotoPath() {
return topPhotoPath;
}
public String getBottomPhotoPath() {
return bottomPhotoPath;
}
public Long getTopId() {
return topId;
}
public Long getBottomId() {
return bottomId;
}