我试图在其他类中调用一个类的静态函数,例如 java,但是在 kotlin 中我无法创建静态函数,我必须创建一个必须在其中定义我的函数的伴侣对象,但是在执行此操作时我不是能够访问父类变量,有什么办法可以在 kotlin 中实现这一点。
class One {
val abcList = ArrayList<String>()
companion object {
fun returnString() {
println(abcList[0]) // not able to access abcList here
}
}
}
class Two {
fun tryPrint() {
One.returnString()
}
}
Run Code Online (Sandbox Code Playgroud)
class One {
val abcList = ArrayList<String>()
companion object {
fun returnString() {
println(abcList[0]) // not able to access abcList here
}
}
}
class Two {
fun tryPrint() {
One.returnString()
}
}
Run Code Online (Sandbox Code Playgroud)
我想像我们在 java 中所做的那样,像第一类的静态函数一样访问有趣的 returnString(),如果有人已经实现了这一点,请帮忙。
在以下任何来源中均未找到插件 [id: 'com.android.library', version: '7.2.0', apply: false]:
使用 --info 或 --debug 选项运行以获得更多日志输出。使用 --scan 运行以获得完整的见解。
org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.resolveToFoundResult(DefaultPluginRequestApplicator.java:221)
在 org.jetbrains.plugins.gradle.model.ProjectImportAction.execute(ProjectImportAction.java:42)
如果有人解决了这个问题,请告诉我,提前致谢
您好,我正在努力将我的应用程序兼容性更新为 android 10 和 11,之前我将我的应用程序设置为默认短信应用程序并从我的应用程序接收和发送新短信,更改默认短信应用程序的意图在 android 10 以下工作正常,但它不是显示更改 Android 10 上弹出的默认短信应用程序
val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName)
startActivity(intent)
Run Code Online (Sandbox Code Playgroud)
如果有人知道 android 10 发生了什么变化,请提出建议,因为我无法在developer.android.com 上找到任何更改,提前致谢
在 kotlin 中,我试图在子类中使用父类变量,但我无法使用它们,因为我是 kotlin 的新手,我不明白如何简单地做到这一点
我正在尝试访问 sharedPerfernces 并获取但它给了我 null
class webViewActivity : AppCompatActivity{
internal var shared_preferences: SharedPreferences? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
shared_preferences = this.getPreferences(Context.MODE_PRIVATE)
mContext = this
}
class JavaScriptInterface(private val mContext: Context) {
@JavascriptInterface
fun exampleGet(path: String): String {
return webViewActivity().shared_preferences!!.getString(path, "")
//here shared_perferences is null
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能在子类中没有父类的构造函数的情况下访问父类变量。给我一些建议,一点帮助将不胜感激
我正在用核心PHP开发一个项目,并且我想在项目中使用雄辩的查询结构来简化设置mySQL连接和执行mySQL查询的过程。
在Java中,如果我有一个类,并且在其中有另一个子类,则子类可以访问其父类方法,但是例如,这在kotlin中给出了错误
class A {
static int methodSum(int a, int b) {
return a+b;
}
static final class Try {
void tryPrint() {
System.Out.println(methodSum(2,3).toString())
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是同样,我无法在Kotlin中实现它给我带来的错误。什么是实现这一目标的最佳方法。