简体   繁体   English

android studio“错误:找不到或加载主类”在Kotlin文件的主要方法中

[英]android studio “error: can't find or load main class” in Kotlin file's main method

In my Android Studio project's java module,I used to code java's main method is OK,But when i convert it to Kotlin code ,like this : 在我的Android Studio项目的Java模块中,我曾经编码过Java的主要方法是可以的,但是当我将其转换为Kotlin代码时,就像这样:

fun main(args: Array<String>) {
    println("i am kotlin")
}

When i executed it, the error happend:"error: can't find or load main class". 当我执行它时,发生了错误:“错误:找不到或加载主类”。 I have tried to find some way to solve it ,for example:let the main method outside the class,But it does not work. 我试图找到某种方法来解决它,例如:让main方法在类外,但是它不起作用。 I want to know whether it is bebcause that my configuration has some problems. 我想知道是否是因为我的配置有问题。 When i use IntelliJ IDEA i build a kotlin project ,the main method is OK. 当我使用IntelliJ IDEA构建一个Kotlin项目时,主要方法是确定。 so what is my problem in my Android studio's java module in kotlin code. 那么我在Android Studio的kotlin代码中的java模块中遇到了什么问题。

In kotlin, the main function should be outside of a class which didn't work for you. 在kotlin中,主要功能应该在对您不起作用的类之外​​。 So when you're trying to use the main function of a class it has to be wrapped up by companion object also be annotated by @JvmStatic . 因此,当您尝试使用类的主函数时,它必须由伴随对象包装起来,并由@JvmStatic进行注释。 So the code block will look like this. 因此,代码块将如下所示。

class AnythingYouWant {

    companion object {
       @JvmStatic
       fun main(args: Array<String>) {
          println("i am kotlin")
       }
    }
}

Another thing to remind you that, 另一件事提醒您,

The main method isn't the entry point in Android like in Java 主要方法不是Android中像Java中的切入点

because it uses completely different build configuration in Android Studio project than the IntelliJ project. 因为它在Android Studio项目中使用与IntelliJ项目完全不同的构建配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio:“错误:找不到或加载主类” - Android Studio : “Error: Could not find or load main class” Android 构建:JarJar 无法找到或加载主类 - Android building: JarJar can't find or load main class Android Studio 未启动:无法找到或加载主 class com.intellij.idea.Main - Android Studio doesn't launch: Could not find or load main class com.intellij.idea.Main 在 Android Studio 中找不到主类错误 - Could not find main class error in Android Studio Android Studio 中的配置 - 无法找到或加载主 class - Configuration in Android Studio - Could not find or load Main class 错误:将 android 工作室更新到 4.1 后,无法在 flutter 中找到或加载主 class - Error: Could not find or load main class in flutter after updating android studio to 4.1 flutter 医生 --android-licenses 错误:无法找到或加载主 class Studio - flutter doctor --android-licenses Error: Could not find or load main class Studio 没有 Android studio 的 Android SDK 会报错“Error: Unable to find or load the main ZA2F2F2E3C5E54C0AB3D375D660B3F896F6AZ” Studio 会报错“Error: Unable to find or load the main ZA2F2F2D660B3F896F6AZ” - The Android SDK without Android studio make an error “Error: Unable to find or load the main class Files\Android\tools\bin\\” 错误:无法在Android Project中找到或加载主类 - Error: Could not find or load main class in Android Project 启动工作室时出错:安装 Android Studio 时“找不到主类 com/intellij/idea/Main” - Error launching studio: "Could not find main class com/intellij/idea/Main" when installing Android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM