Android Studio:运行主方法时出现构建错误

huangapple go评论72阅读模式
英文:

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!");
    }
}
  1. 收到的错误:

构建失败,出现异常。

  • 位置:
    初始化脚本 '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 &quot;Hello&quot;
2. Attempted to run main method:

public class Hello {

    public static void main(String[] args) {
        System.out.println(&quot;Hello World!&quot;);
        }
    }


3: Error I received:

FAILURE: Build failed with an exception.

* Where:
Initialization script &#39;C:\Users\User25\AppData\Local\Temp\Hellos_main__.gradle&#39; line: 21

* What went wrong:
A problem occurred configuring project &#39;:app&#39;.
&gt; Could not create task &#39;:app:Hellos.main()&#39;.
   &gt; SourceSet with name &#39;main&#39; not found.

-----------------------

Things I tried: Ran the &quot;Hello&quot; class with coverage and works just fine. However, I have no idea why I can&#39;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&#39;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&amp;ab_channel=ForTheGreaterGood

<details>
<summary>英文:</summary>

For running your Java code follows the following steps:
1) Go to File &gt; New &gt; 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&amp;ab_channel=ForTheGreaterGood </details>

huangapple
  • 本文由 发表于 2020年9月1日 09:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63680256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定