我有一个Gluon Mobile JavafX项目。
我试图从我的apk中读取资源,但我只是在Android中收到错误。
private void leerConfiguracion() { logger.log(Level.INFO, "ConfiguradorGeneralService:leerConfiguracion: BEGIN ... ");
try
{
File fileLugares = new File(ConfiguradorGeneralService.class.getResource("/com/registrodevisitas/json/configuracion.json").getFile());
logger.log(Level.INFO, "ConfiguradorGeneralService:leerConfiguracion: Leyendo configuración: " + fileLugares.getAbsolutePath());
// create a FileClient to the specified File
FileClient fileClient = FileClient.create(fileLugares);
// create a JSON converter that converts the nodes from a JSON array into language objects
InputStreamIterableInputConverter<ConfiguradorGeneral> converter = new JsonIterableInputConverter<>(ConfiguradorGeneral.class);
// retrieve a list from a ListDataReader created from the FileClient
configuracion = DataProvider.retrieveList(fileClient.createListDataReader(converter));
}
catch(Exception e)
{
logger.log(Level.SEVERE, e.getMessage(), e);
}
logger.log(Level.INFO, "ConfiguradorGeneralService:leerConfiguracion: END ");
}
Android上的错误是:
I/figuradorGeneralService(11481): ConfiguradorGeneralService:leerConfiguracion: BEGIN ...
E/figuradorGeneralService(11481): null
E/figuradorGeneralService(11481): java.lang.NullPointerException
E/figuradorGeneralService(11481): at com.registrodevisitas.services.ConfiguradorGeneralService.leerConfiguracion(ConfiguradorGeneralService.java:50)
E/figuradorGeneralService(11481): at com.registrodevisitas.services.ConfiguradorGeneralService.<init>(ConfiguradorGeneralService.java:34)
E/figuradorGeneralService(11481): at java.lang.Class.newInstanceImpl(Native Method)
E/figuradorGeneralService(11481): at java.lang.Class.newInstance(Class.java:1208)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.injection.Injector.lambda$getDefaultInstanceSupplier$8(Injector.java:233)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.injection.Injector.access$lambda$3(Injector.java)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.injection.Injector$$Lambda$4.apply(Unknown Source)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.injection.Injector.instantiateModelOrService(Injector.java:116)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.injection.Injector.injectMembers(Injector.java:151)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.injection.Injector.injectMembers(Injector.java:136)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.injection.Injector.injectAndInitialize(Injector.java:129)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.injection.Injector.registerExistingAndInject(Injector.java:107)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.injection.Injector.instantiatePresenter(Injector.java:60)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.views.FXMLView.lambda$loadSynchronously$2(FXMLView.java:89)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.views.FXMLView.access$lambda$1(FXMLView.java)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.views.FXMLView$$Lambda$2.call(Unknown Source)
E/figuradorGeneralService(11481): at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:929)
E/figuradorGeneralService(11481): at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
E/figuradorGeneralService(11481): at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
E/figuradorGeneralService(11481): at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
E/figuradorGeneralService(11481): at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
E/figuradorGeneralService(11481): at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
E/figuradorGeneralService(11481): at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
E/figuradorGeneralService(11481): at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.views.FXMLView.loadSynchronously(FXMLView.java:91)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.views.FXMLView.initializeFXMLLoader(FXMLView.java:100)
E/figuradorGeneralService(11481): at com.airhacks.afterburner.views.FXMLView.getView(FXMLView.java:112)
E/figuradorGeneralService(11481): at com.registrodevisitas.RegistroDeVisitas.lambda$init$0(RegistroDeVisitas.java:55)
E/figuradorGeneralService(11481): at com.registrodevisitas.RegistroDeVisitas.access$lambda$0(RegistroDeVisitas.java)
E/figuradorGeneralService(11481): at com.registrodevisitas.RegistroDeVisitas$$Lambda$1.get(Unknown Source)
E/figuradorGeneralService(11481): at com.gluonhq.impl.charm.a.d.a.a(SourceFile:32)
E/figuradorGeneralService(11481): at com.gluonhq.charm.glisten.application.MobileApplication.a(SourceFile:620)
E/figuradorGeneralService(11481): at com.gluonhq.charm.glisten.application.MobileApplication.switchView(SourceFile:391)
E/figuradorGeneralService(11481): at com.gluonhq.charm.glisten.application.MobileApplication.switchView(SourceFile:368)
E/figuradorGeneralService(11481): at com.gluonhq.charm.glisten.application.MobileApplication.start(SourceFile:218)
E/figuradorGeneralService(11481): at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$144(LauncherImpl.java:863)
E/figuradorGeneralService(11481): at com.sun.javafx.application.LauncherImpl.access$lambda$8(LauncherImpl.java)
E/figuradorGeneralService(11481): at com.sun.javafx.application.LauncherImpl$$Lambda$9.run(Unknown Source)
E/figuradorGeneralService(11481): at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$157(PlatformImpl.java:326)
E/figuradorGeneralService(11481): at com.sun.javafx.application.PlatformImpl.access$lambda$6(PlatformImpl.java)
E/figuradorGeneralService(11481): at com.sun.javafx.application.PlatformImpl$$Lambda$7.run(Unknown Source)
E/figuradorGeneralService(11481): at com.sun.javafx.application.PlatformImpl.lambda$null$155(PlatformImpl.java:295)
E/figuradorGeneralService(11481): at com.sun.javafx.application.PlatformImpl.access$lambda$18(PlatformImpl.java)
E/figuradorGeneralService(11481): at com.sun.javafx.application.PlatformImpl$$Lambda$19.run(Unknown Source)
E/figuradorGeneralService(11481): at java.security.AccessController.doPrivileged(AccessController.java:52)
E/figuradorGeneralService(11481): at com.sun.javafx.application.PlatformImpl.lambda$runLater$156(PlatformImpl.java:294)
E/figuradorGeneralService(11481): at com.sun.javafx.application.PlatformImpl.access$lambda$5(PlatformImpl.java)
E/figuradorGeneralService(11481): at com.sun.javafx.application.PlatformImpl$$Lambda$6.run(Unknown Source)
E/figuradorGeneralService(11481): at com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:92)
E/figuradorGeneralService(11481): at com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:51)
E/figuradorGeneralService(11481): at java.lang.Thread.run(Thread.java:8
I/figuradorGeneralService(11481): ConfiguradorGeneralService:leerConfiguracion: END
此文件位于RESOURCE / COM ....
此方法在Windows上完美运行。
任何帮助都会很好。谢谢。
答案 0 :(得分:0)
在Android(和iOS)上,您无法像在桌面上一样直接访问类路径资源作为文件。相反,您需要使用InputStream访问资源。这适用于所有支持的平台(Android,iOS和桌面)。
尝试使用以下代码访问您的资源:
BasicInputDataSource source = new BasicInputDataSource(ConfiguradorGeneralService.class.getResourceAsStream("/com/registrodevisitas/json/configuracion.json"));
InputStreamIterableInputConverter<ConfiguradorGeneral> converter = new JsonIterableInputConverter<>(ConfiguradorGeneral.class);
configuracion = DataProvider.retrieveList(new InputStreamListDataReader<>(source, converter));