我尝试引用java中的fragment类中的Kotlin类,结果出现错误,提示无法找到kotlin类的符号。
碎片类
如果您的浏览器是一个虚拟机,那么您可以使用一个虚拟机来创建一个虚拟机。如果您想查看一个新的页面,请点击这里。
//Instructions audio
instruct = (ImageButton) rootview.findViewById(R.id.soundBtn);
MediaPlayer.create(getActivity(), R.raw.instructions);
//Click go button to explore flashcards
go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getActivity(), Flashcards1.class));
}
});
return rootview;
}
}
这就是错误所在
startActivity(new Intent(getActivity(), Flashcards1.class));
构建.gradle应用程序
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.example.salitongue_updated"
minSdk 27
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.firebase:firebase-database:20.0.5'
implementation 'com.google.firebase:firebase-firestore:24.2.2'
implementation 'com.google.firebase:firebase-auth:21.0.7'
implementation 'androidx.navigation:navigation-fragment:2.5.1'
implementation 'androidx.navigation:navigation-ui:2.5.1'
implementation "androidx.fragment:fragment-ktx:1.6.0-alpha02"
//testImplementation 'junit:junit:4.13.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
build.gradle
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.13'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10"
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
1条答案
按热度按时间wxclj1h51#
我认为你的问题是你没有为你的
app
模块设置Kotlin。你已经把插件依赖性添加到你的build.gradle
主模块中,但是您没有在 *app
模块 *build.gradle
中应用该插件。您也没有将kotlin-stdlib
依赖项添加到该模块中。(您必须向下滚动一点到代码片段)。我不太了解Gradle以及配置构建文件的方法,因此您可能以不同的方式设置了它,这没问题-但您 * 应该 * 能够看到Java without doing anything special中的Kotlin类文件:
Kotlin代码可以很容易地从Java中调用。例如,Kotlin类的示例可以无缝地创建并在Java方法中操作。
所以看起来好像它只是没有设置好,这就是为什么它对你不起作用。如果Kotlin在IDE中工作(就像在代码完成时弹出),但当你试图构建它时却没有,那听起来好像IDE插件设置好了,但你项目中的东西没有。我不确定,但希望它能有所帮助!