简体   繁体   English

Android 工作室 - kotlin class 在 java 文件中实施时未找到 - 错误“找不到符号类”

[英]Android studio - kotlin class not found when implementing in java file - error "cannot find symbol class"

I am trying to use a kotlin class in my java class in android studio.我正在尝试在 android 工作室的 java class 中使用 kotlin class。 But I get the following error twice...但是我两次收到以下错误...

"cannot find symbol class KotlinClass" “找不到符号 class KotlinClass”

KotlinClass is the file name of the kotlin class I am trying to implement. KotlinClass 是我要实现的 kotlin class 的文件名。

Both the java and kotlin file are in the same package. java和kotlin文件都在同一个package中。

I have tried to put them in different packages but it caused me more problems and so i kept them in the same package but i might have done this so if this is a potential solution, i can try again but i would need more detailed steps if possible.我曾尝试将它们放在不同的包中,但它给我带来了更多问题,所以我将它们放在同一个 package 中,但我可能已经这样做了,所以如果这是一个潜在的解决方案,我可以再试一次,但我需要更详细的步骤,如果可能的。

This error occurs in the build tab.此错误发生在构建选项卡中。

I have looked into this quite a bit but nothing seems to work.我对此进行了相当多的调查,但似乎没有任何效果。

This is my java class这是我的 java class

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        KotlinClass x = new KotlinClass();
        x.kotlinMethod();
    }
}

This is my kotlin class这是我的 kotlin class

lass KotlinClass {
    fun kotlinMethod() {
        println("You did it again!!!")
    }
}

Im very new to android studio, any help will be appreciated我是 android 工作室的新手,我们将不胜感激

I believe that your issue is that you have created the project as a Java project and a Java project doesn't support Kotlin directly.我认为您的问题是您已将项目创建为 Java 项目,而 Java 项目不直接支持 Kotlin。

If you create the project as a Kotlin Project eg如果您将项目创建为 Kotlin 项目,例如

在此处输入图像描述

Then Kotlin supports Java from the onset.那么Kotlin一开始就支持Java。 The only issue is that the Activity will then be in Kotlin.唯一的问题是活动将在 Kotlin 中。

To circumvent this, as was done below, you could add another Activity set it to be Java, and then use this to replace the Kotlin activity (not sure why other than to do it though).为了避免这种情况,如下所示,您可以添加另一个活动,将其设置为 Java,然后使用它来替换 Kotlin 活动(虽然不知道为什么不这样做)。

The following demonstrates:-以下说明:-

在此处输入图像描述

  • ie the project comppiled and ran on the emulator.即项目编译并在模拟器上运行。
  • as can be seen MainActivity (with some manipulation) is Java可以看出 MainActivity(经过一些操作)是 Java
  • KotlinClass is Kotlin KotlinClass 是 Kotlin

Create Kotlin class like this and keep rest the same.像这样创建Kotlin class并保持 rest 相同。

在此处输入图像描述

For this issue, I have a simple solution that works for me.对于这个问题,我有一个适合我的简单解决方案。

add below id in build.gradle app level gradle在 build.gradle 应用程序级别 gradle 中添加以下 ID

 plugins { id 'org.jetbrains.kotlin.android' }

 plugins { id 'org.jetbrains.kotlin.android' version '1.6.21' apply false }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM