英文:
Android Studio: Build Error When Running Main Method
问题
面临的问题:
1. 创建了一个名为“Hello”的新Java类
2. 尝试运行main方法:
```java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
- 收到的错误:
构建失败,出现异常。
-
位置:
初始化脚本 'C:\Users\User25\AppData\Local\Temp\Hellos_main__.gradle' 第 21 行 -
出了什么问题:
在配置项目':app'时出现问题。无法创建任务':app:Hellos.main()'。
找不到名称为'main'的SourceSet。
我尝试过的事情:使用覆盖率运行了“Hello”类,结果正常。然而,我不知道为什么不能简单地运行这段代码。我可能已经花了大约2到3个小时在线上搜索如何解决这个问题,但是找不到解决方案。在Android Studios论坛上有人遇到了同样的问题,但没有人提供解决方案。非常感谢您的帮助。提前致谢。
<details>
<summary>英文:</summary>
Issue I am facing:
1. Created new Java Class called "Hello"
2. Attempted to run main method:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
3: Error I received:
FAILURE: Build failed with an exception.
* Where:
Initialization script 'C:\Users\User25\AppData\Local\Temp\Hellos_main__.gradle' line: 21
* What went wrong:
A problem occurred configuring project ':app'.
> Could not create task ':app:Hellos.main()'.
> SourceSet with name 'main' not found.
-----------------------
Things I tried: Ran the "Hello" class with coverage and works just fine. However, I have no idea why I can't simply run the code. I have probably spent a good 2 to 3 hours searching online on how to resolve this issue but couldn't find a solution. Someone had the same problem on the Android Studios forum but nobody got back with a solution. Would appreciate your help. Thanks in advance.
[Link to exercise I am trying to follow][1]
[1]: https://i.stack.imgur.com/gOWwO.jpg
</details>
# 答案1
**得分**: 1
在Android中没有`main()`方法。主入口点是在AndroidManifest.xml中指定的一个Activity(通常称为`MainActivity`)。
<details>
<summary>英文:</summary>
There is no `main()` method in Android. the main entry point is an Activity as specified in AndroidManifest.xml (usually called `MainActivity`).
</details>
# 答案2
**得分**: 0
运行您的Java代码,请按照以下步骤进行:
1) 转到 文件 > 新建 > 新建模块,然后选择 Java Library
2) 创建一个Java库,并在其中添加您的代码
3) 通过点击绿色箭头来运行代码,如下所示:
[![在这里输入图片描述][1]][1]
您可以在此[链接][2]中找到更多信息。
[1]: https://i.stack.imgur.com/NS2IU.png
[2]: https://www.youtube.com/watch?v=SUFPgKsYWvA&ab_channel=ForTheGreaterGood
<details>
<summary>英文:</summary>
For running your Java code follows the following steps:
1) Go to File > New > New Module and select Java Library
2) Create a Java Library and add your code inside it
3) Run the code by clicking the green arrow as follows:
[![enter image description here][1]][1]
You can find more information in this [2]
[1]: https://i.stack.imgur.com/NS2IU.png
[2]: https://www.youtube.com/watch?v=SUFPgKsYWvA&ab_channel=ForTheGreaterGood
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论