英文:
Get value from java application class in kotlin service
问题
以下是翻译好的部分:
我有一个 Java 应用程序类,我想在 Kotlin 服务中从它获取一个值。
我尝试使用以下代码来实现,但返回值为空:
myValue = (application as App).getNumValue
部分代码示例:
应用程序类:
private String numValue = "";
public String getNumValue() {
return numValue;
}
public void setNumValue(String numValue) {
this.numValue = numValue;
}
用于设置值的 Java 活动:
((App) this.getApplication()).setNumValue(enNum);
Kotlin 服务和读取值:
myValue = (application as App).getNumValue()
Toast.makeText(this, myValue + "", Toast.LENGTH_SHORT).show()
英文:
i have a java application class and i want to get a value from it in kotlin service .
i try to do it with this code but it's return null
myValue = (application as App).getNumValue
some of my code
Application Class:
private String numValue = "";
public String getNumValue() {
return numValue;
}
public void setNumValue(String numValue) {
this.numValue = numValue;
}
java Activity for set value :
((App) this.getApplication()).setNumValue(enNum);
Kotlin Service And Read Value :
myValue = (application as App).getNumValue()
Toast.makeText(this, myValue +"", Toast.LENGTH_SHORT).show()
答案1
得分: 0
以下是翻译好的部分:
我已经解决了我的问题:
val myValue = (this.application as App).getNumValue()
Toast.makeText(this, myValue + "", Toast.LENGTH_SHORT).show()
英文:
I was solved my problem with :
val myValue = (this.application as App).getNumValue()
Toast.makeText(this, myValue +"", Toast.LENGTH_SHORT).show()
专注分享java语言的经验与见解,让所有开发者获益!
评论