无法从核心模块类LibGDX Android Studio访问Java对象



我正在尝试创建一个带有该项目内部游戏的Android项目。我找到了一个名为LibGDX的Java开发游戏框架。当我试图使用它时,我遇到了几个问题。首先,在core模块中扩展ApplicationAdapter的类中,我无法像String那样调用所有Java对象,错误说:

Cannot resolve symbol 'String'

第二个问题是所有的Gdx对象有一些错误,如:

Cannot access java.lang.Object
Cannot resolve constructor 'Texture(java.lang.String)'

奇怪的是:第二个问题我仍然可以安装和运行它。我一直在试图修复Gradle,但它不起作用。这是我的build.gradle

buildscript {

repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.1'
}
}
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"

version = '1.0'
ext {
appName = "TestGame"
gdxVersion = '1.10.0'
roboVMVersion = '2.3.12'
box2DLightsVersion = '1.5'
ashleyVersion = '1.7.3'
aiVersion = '1.8.2'
gdxControllersVersion = '2.1.0'
}
repositories {
mavenLocal()
mavenCentral()
google()
gradlePluginPortal()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":android") {
apply plugin: "com.android.application"
configurations { natives }
dependencies {
implementation project(":core")
api "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.+'
testImplementation 'commons-logging:commons-logging:1.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

}
}
project(":core") {
apply plugin: "java-library"
dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"

}
}

类:

public class TestGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
}
}

有什么建议吗?

也许这是一个基本的问题,但是这个LibGDX的东西对我来说有点晦涩。

我以前遇到过这个问题,这个解决方案对我很有效。去你的锅子那儿。属性文件。写org.gradle.java.home=C:/Program Files/Java/jdk-11.0.12或者你的JDK文件夹所在的位置,比如org.gradle.java。home=(jdk文件夹所在的位置)。我也在网上发现了一个类似的问题,但这是专门为IntelliJ和Android Studio的,所以这可能不适合你。如果您不使用libGDX设置,则此解决方案可能不起作用,因为它只在libGDX上进行了测试。我对libGDX也很陌生。https://intellij-support.jetbrains.com/hc/en-us/community/posts/207039495-Cannot-resolve-symbol-String-。这篇文章是旧的

是的,我花了两天时间来解决这个问题。就我而言,我刚刚更新了Android Studio,一切都运行良好。