为什么 Android Studio 不允许我创建 Java 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20588040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why Android Studio doesn't allow me to create Java Classes?
提问by Stefano Munarini
Starting from the 2 days ago update to Android Studio, it doesnt let me create Java classes anymore. And the current classes are now with a strange symbol.
从 2 天前更新到 Android Studio 开始,它不再让我创建 Java 类。而当前的类现在带有一个奇怪的符号。
I tried to export, import many and many times with different config but never worked. Any advice?
我尝试使用不同的配置多次导出、导入,但从未奏效。有什么建议吗?
Here are two screenshot
这是两个截图
UPDATE 1:
UPDATE 1:
Here is the structure of my Android Studio project. AndroidManifest is in the right place.
这是我的 Android Studio 项目的结构。AndroidManifest 是在正确的地方。
回答by So Many Goblins
Right click on the src folder, Mark Directory As -> Source Root.
右键单击 src 文件夹,将目录标记为 -> 源根。
回答by Paul
Android Studio 1.0 Update:
Android Studio 1.0 更新:
In project view of Android Studio please check for “java”folder under src.
在 Android Studio 的项目视图中,请检查src下的“java”文件夹。
Usually there will be two folders automatically created by Android Studio under src -
通常Android Studio在src下会自动创建两个文件夹——
- androidTest (This is used for writing test cases),
- main (This is used for writing your application source.)
- androidTest(用于编写测试用例),
- main (这用于编写您的应用程序源。)
Each of these should contain a "java" folder.
每一个都应该包含一个“java”文件夹。
If javafolder is not there, you can create it with right click on main(or androidTest) as shown below
如果java文件夹不存在,您可以通过右键单击main(或androidTest)来创建它,如下所示
To add a new java class, please right click on the "java folder"and select "java class".
要添加新的 java 类,请右键单击“java 文件夹”并选择“java 类”。
Regards,
问候,
Paul
保罗
回答by GyRo
I tried the solution above but couldn't find the menu item to set the 'src' as the Source Root. So I managed to solve it by making a similar setting in the app's build.gradle file. I added this:
我尝试了上面的解决方案,但找不到将“ src”设置为Source Root的菜单项。所以我设法通过在应用程序的 build.gradle 文件中进行类似设置来解决它。我加了这个:
android {
...
sourceSets {
main.java.srcDirs += 'src/main/java'
}
}
回答by dell116
A bit of an edge-case, but I found my way here because I couldn't create a java package.
有点边缘情况,但我在这里找到了方法,因为我无法创建 java 包。
The reason being my project has multiple build variants (Google / Amazon build variants with different source folders) and I was trying to create a package in a non-active build variant java source folder.
原因是我的项目有多个构建变体(具有不同源文件夹的 Google/Amazon 构建变体),我试图在非活动构建变体 java 源文件夹中创建一个包。
As of Android Studio 3.1, you can't create a new package if you're trying to do so in a build-variant that's not the current build variant you're working with.
从 Android Studio 3.1 开始,如果您尝试在不是您正在使用的当前构建变体的构建变体中创建新包,则无法创建新包。
Basically, you have to switch to that build variant before any type of compilation change can be made.
基本上,您必须在进行任何类型的编译更改之前切换到该构建变体。