gpt4 book ai didi

android - 在 android studio 中运行带有 main 函数的单个 kotlin 类

转载 作者:IT老高 更新时间:2023-10-28 13:28:00 29 4
gpt4 key购买 nike

我正在尝试熟悉 Kotlin 以在我的 Android 应用程序中使用。所以首先我想尝试一些简单的 kotlin 示例,只是为了熟悉 kotlin 的语法。

我在我的 android 项目中使用 main 方法创建了一个名为 Main.kt 的类。

fun main(args: Array<String>) {
println("Hello World"); }

Android Studio 在 main 方法的左侧显示一个 kotlin 图标,当我单击此图标时,它会显示以下三个选项:

1) 运行 Mainkt

2) 调试 Mainkt

3) 在覆盖范围内运行 Mainkt

我选择了第一个,但它把我扔了

Exception in thread "main" java.lang.ClassNotFoundException: com.vikalp.kotlin.MainKt
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:107)

我被这么一个小问题困住了。让我知道你们中是否有人遇到过这样的问题以及解决方案是什么。

最佳答案

更新:

方法一:

现在您可以使用空的 main() 方法创建一个 kotlin 文件,然后您可以在 android studio (AS 版本) 中使用左侧(第 7 行)左侧的运行图标直接运行代码: 3.5.3 ) 编辑器喜欢

enter image description here

这将在内部创建带有 PSVM 方法(和所需结构)的 TestKt(文件名)class 来执行代码。

运行此文件的演示配置(自动生成)如下所示

enter image description here

方法 2(使用 Scratch 文件,在 AS 3.6 上测试):

  1. 在项目导航面板中选择 Project View 。
  2. New -> Scratch File -> Kotlin 创建一个 Kotlin 暂存文件 enter image description here
  3. 现在添加您的代码并在右侧面板中查看结果

    enter image description here

Android studio (intellij) 提供 REPL(Real Eval Print Loop) 工具来编写和执行 kotlin 代码。

  1. Tool -> kotlin -> kotlin REPL 的方式打开 kotlin REPL

enter image description here

  1. 编写你的代码

enter image description here

  1. 按command + enter(在mac上)执行你的代码(注意不同平台的组合键)

要么写代码,要么导入

enter image description here

提示:

  • 更改源代码后重新构建项目
  • 使用箭头键返回历史

关于android - 在 android studio 中运行带有 main 函数的单个 kotlin 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44823064/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com