尝试使用 Gluon 访问 GPS,但应用程序意外关闭

Trying to access GPS with Gluon, but app shuts down unexpectedly

我一直想知道我的 GPS 状态是否已启用,如果未启用,它会启用并给我位置坐标。但是每当我启动应用程序并单击按钮以获取坐标时,应用程序都会意外关闭。

我的代码如下:

button.setOnAction(e->{
        PositionService positionService = Services.get(PositionService.class).orElseThrow(() -> new RuntimeException("PositionService not available."));
        positionService.positionProperty().addListener((obs, ov, nv) -> MobileApplication.getInstance().showMessage("Latest known GPS coordinates from device: " + nv.getLatitude() + ", " + nv.getLongitude()));
    }); 

我在 Gluon Mobile 的文档中找到了代码。

这是我的 build.gradle 文件。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.3.10'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

mainClassName = 'com.gluonapplication.test.GluonTestApplication'

dependencies {
    compile 'com.gluonhq:charm:4.4.1'
    compile 'com.gluonhq:charm-down-common:2.0.0';
    desktopRuntime 'com.gluonhq:charm-down-desktop:2.0.0';
    androidRuntime 'com.gluonhq:charm-down-android:2.0.0';
    iosRuntime 'com.gluonhq:charm-down-ios:2.0.0';
    compile group: 'com.gluonhq', name: 'charm-down-plugin-position', 
    version: '3.7.0'
}

jfxmobile {
    downConfig {
        version = '3.7.0'
        // Do not edit the line below. Use Gluon Mobile Settings in your 
project context menu instead
        plugins 'display', 'lifecycle', 'statusbar', 'storage', 'position'
    }
    android {
        manifest = 'src/android/AndroidManifest.xml'
        androidSdk = 'C:/Users/Mainul/AppData/Local/Android/Sdk'
    }
    ios {
        infoPList = file('src/ios/Default-Info.plist')
        forceLinkClasses = [
                'com.gluonhq.**.*',
                'javax.annotations.**.*',
                'javax.inject.**.*',
                'javax.json.**.*',
                'org.glassfish.json.**.*'
        ]
    }
}

注意:我还在 AndroidManifest.xml 文件中添加了 Position 权限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

您混淆了 Charm Down 的新旧依赖项。

dependencies {
    compile 'com.gluonhq:charm:4.4.1'

    // (1)
    compile 'com.gluonhq:charm-down-common:2.0.0';
    desktopRuntime 'com.gluonhq:charm-down-desktop:2.0.0';
    androidRuntime 'com.gluonhq:charm-down-android:2.0.0';
    iosRuntime 'com.gluonhq:charm-down-ios:2.0.0';

    // (2)
    compile group: 'com.gluonhq', name: 'charm-down-plugin-position', version: '3.7.0'
}

首先,最新的 Charm Down 版本是 3.7.0,因此不再需要 (1) 中的所有“2.0.0”依赖项。

此外,downConfig 设法添加了所需的 Charm Down 依赖项,因此您无需将它们包含在 dependencies {}、(2).

只需删除 (1) 和 (2) 中的那些:

dependencies {
    compile 'com.gluonhq:charm:4.4.1'
} 

清理并再次部署。