JavaFX的胶子保存XML文件 [英] Javafx Gluon save XML File

查看:763
本文介绍了JavaFX的胶子保存XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要救Android上的XML文件之前,我问了一个问题,如何让现在我卡中的文件;(我用的胶子(NetBeans插件和已经做了一切什么他说:<一href=\"http://stackoverflow.com/questions/34369076/javafxportsgluon-plugin-saving-files-on-android/34370205?noredirect=1#comment56490775_34370205\">JavaFXPorts(Gluon插件)保存文件在Android

您可以帮助我,我要保存在Android XML文件,这是我的code尚未:

 公共静态无效loadConfig()抛出JAXBException {
    如果(ConfigFile.length()!= 0){
        JAXBContext中的JAXBContext = JAXBContext.newInstance(Info.class);
        解组jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        信息=(信息)jaxbUnmarshaller.unmarshal(通过ConfigFile);
    }其他{
        信息=新信息();
    }}公共静态无效saveconfig的(){
    尝试{
        info.setAge(10);
        JAXBContext而上下文= JAXBContext.newInstance(Info.class);
        编组M = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
        m.marshal(资讯,通过ConfigFile);
    }赶上(PropertyException前){
        。Logger.getLogger(STM_WU_LABOR.class.getName())日志(Level.SEVERE,空,前);
    }赶上(JAXBException前){
        。Logger.getLogger(STM_WU_LABOR.class.getName())日志(Level.SEVERE,空,前);
    }
}公共文件的GetFile()抛出IOException
    文件路径= PlatformFactory.getPlatform()getPrivateStorage()。    返回新的文件(路径,config.xml文件);
}

下面是我如何称呼它:

 通过ConfigFile =的GetFile();
    如果(!ConfigFile.exists()){
        ConfigFile.createNewFile();
    }

这是信息类:

 包com.stm_wu_labor;进口javax.xml.bind.annotation.XmlRootElement;/ **
 *
 * @author Merduma
 * /
@XmlRootElement
公共类信息{    信息(){    }    公共信息(字符串性别,年龄INT,诠释SchoolClass){
        this.gender =性别;
        this.age =年龄;
        this.SchoolClass = SchoolClass;
    }    私人字符串性别;    公共字符串getGender(){
        返回性别;
    }    公共无效setGender(字符串性别){
        this.gender =性别;
    }    公众诠释getAge(){
        返回年龄;
    }    公共无效setAge(INT年龄){
        this.age =年龄;
    }    公众诠释getSchoolClass(){
        返回SchoolClass;
    }    公共无效setSchoolClass(INT SchoolClass){
        this.SchoolClass = SchoolClass;
    }
    私人诠释年龄;
    私人诠释SchoolClass;
}

在PROGRAMM停止当我尝试启动它在Android上,请帮我

 这是亚行logcat(我希望这是相关的):
I /的System.out(13007):该PROGRAMM开始
I /的System.out(13007):成功与开始
I /的System.out(13007):加入信息
W /艺术(13007):发现catch块时,未解决的异常类:javax.xml.bind.PropertyException
W /艺术(13007):发现catch块时,未解决的异常类:javax.xml.bind.JAXBException
W / System.err的(13007):异常在应用启动方法
I /的System.out(13007):QuantumRenderer:关机
W / System.err的(13007):java.lang.reflect.InvocationTargetException
W / System.err的(13007):在java.lang.reflect.Method.invoke(本机方法)
W / System.err的(13007):在java.lang.reflect.Method.invoke(Method.java:372)
W / System.err的(13007):在javafxports.android.DalvikLauncher $ 1.run(DalvikLauncher.java:188)
W / System.err的(13007):在java.lang.Thread.run(Thread.java:818)
W / System.err的(13007):了java.lang.RuntimeException:通过引起异常的应用启动方法
W / System.err的(13007):在com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
W / System.err的(13007):在com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 137(LauncherImpl.java:182)
W / System.err的(13007):在com.sun.javafx.application.LauncherImpl.access $拉姆达$ 1(LauncherImpl.java)
W / System.err的(13007):在com.sun.javafx.application.LauncherImpl $$ LAMBDA $ 2.run(来源不明)
W / System.err的(13007):... 1更多
W / System.err的(13007):java.lang.NoClassDefFoundError的:致失败分辨率:Ljavax / XML /绑定/ JAXBContext中;
W / System.err的(13007):在com.stm_wu_labor.STM_WU_LABOR.saveConfig(STM_WU_LABOR.java:161)
W / System.err的(13007):在com.stm_wu_labor.STM_WU_LABOR.start(STM_WU_LABOR.java:65)
W / System.err的(13007):在com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 144(LauncherImpl.java:863)
W / System.err的(13007):在com.sun.javafx.application.LauncherImpl.access $拉姆达$ 8(LauncherImpl.java)
W / System.err的(13007):在com.sun.javafx.application.LauncherImpl $$ LAMBDA $ 9.run(来源不明)
W / System.err的(13007):在com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 157(PlatformImpl.java:326)
W / System.err的(13007):在com.sun.javafx.application.PlatformImpl.access $拉姆达$ 6(PlatformImpl.java)
W / System.err的(13007):在com.sun.javafx.application.PlatformImpl $$ LAMBDA $ 7.run(来源不明)
W / System.err的(13007):在com.sun.javafx.application.PlatformImpl.lambda $空$ 155(PlatformImpl.java:295)
W / System.err的(13007):在com.sun.javafx.application.PlatformImpl.access $拉姆达$ 18(PlatformImpl.java)
W / System.err的(13007):在com.sun.javafx.application.PlatformImpl $$ LAMBDA $ 19.run(来源不明)
W / System.err的(13007):在java.security.AccessController.doPrivileged(AccessController.java:52)
W / System.err的(13007):在com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 156(PlatformImpl.java:294)
W / System.err的(13007):在com.sun.javafx.application.PlatformImpl.access $拉姆达$ 5(PlatformImpl.java)
W / System.err的(13007):在com.sun.javafx.application.PlatformImpl $$ LAMBDA $ 6.run(来源不明)
W / System.err的(13007):在com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:92)
W / System.err的(13007):在com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:51)
W / System.err的(13007):... 1更多
W / System.err的(13007):抛出java.lang.ClassNotFoundException:产生的原因没有在路径上找到的类javax.xml.bind.JAXBContext:DexPathList [zip文件\"/data/app/com.stm_wu_labor-2/base.apk\"],nativeLibraryDirectories=[/data/app/com.stm_wu_labor-2/lib/arm, /供应商/ lib中/系统/ lib目录]
W / System.err的(13007):在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
W / System.err的(13007):在java.lang.ClassLoader.loadClass(ClassLoader.java:511)
W / System.err的(13007):在java.lang.ClassLoader.loadClass(ClassLoader.java:469)
W / System.err的(13007):... 18个
W / System.err的(13007):燮pressed:抛出java.lang.ClassNotFoundException:javax.xml.bind.JAXBContext
W / System.err的(13007):在java.lang.Class.classForName(本机方法)
W / System.err的(13007):在java.lang.BootClassLoader.findClass(ClassLoader.java:781)
W / System.err的(13007):在java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
W / System.err的(13007):在java.lang.ClassLoader.loadClass(ClassLoader.java:504)
W / System.err的(13007):... 19个
W / System.err的(13007):java.lang.NoClassDefFoundError的:致类没有使用引导类加载器找到;没有堆栈可用


解决方案

在移植了做工精细的桌面到Android / iOS版,而使用第三方的依赖的Java / JavaFX的项目,你应该知道,并不是所有的这些依赖关系可以导出。

例如,在MySQL的方面,它可以更方便地使用的SQLite。勾选此帖子关于使用根据不同的平台不同的SQLite库。

在XML的情况下,它可以是相同的情况下。这问题已经提到了在Android上使用JAXB的缺点,并提出了一些方案。

使用胶子魅力向下开源库(检查将其添加到构建脚本),您可以轻松管理局部属性在任何平台的文件,使用 SettingService ,它允许存储和检索属性:

  SettingService设置= PlatformFactory.getPlatform()getSettingService()。settings.store(Info.Age,10);
...
如果(settings.retrieve(Info.Age)!= NULL){
    INT年龄=的Integer.parseInt(settings.retrieve(Info.Age));
}

最后,当你发现你的Andr​​oid应用程序的任何问题,请与调试亚行,找出可能的例外。

亚行位于Android SDK的文件夹下, SDK /平台的工具/

您的移动设备连接到USB端口,并在命令行中键入:

  ./亚行logcat -v threadtime

启动应用程序,并找出消息在控制台中的相关部分,可能出现的异常或日志消息(使用的System.out.println 通信System.err.println )。

I Want to save a xml File on Android before I asked a questions how to Get a File now I'm stuck ;( I use Gluon(Netbeans plugin and did already everything what He said: JavaFXPorts(Gluon Plugin) Saving Files on Android

Can you help me I want to save a XML File on Android, this is my Code yet:

    public static void loadConfig() throws JAXBException {


    if (ConfigFile.length() != 0) {
        JAXBContext jaxbContext = JAXBContext.newInstance(Info.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        info = (Info) jaxbUnmarshaller.unmarshal(ConfigFile);
    } else {
        info = new Info();
    }

}

public static void saveConfig() {
    try {
        info.setAge(10);             
        JAXBContext context = JAXBContext.newInstance(Info.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(info, ConfigFile);
    } catch (PropertyException ex) {
        Logger.getLogger(STM_WU_LABOR.class.getName()).log(Level.SEVERE, null, ex);
    } catch (JAXBException ex) {
        Logger.getLogger(STM_WU_LABOR.class.getName()).log(Level.SEVERE, null, ex);
    }


}

public File getFile() throws IOException {
    File path = PlatformFactory.getPlatform().getPrivateStorage();

    return new File(path, "config.xml");
}

Here is how i call it:

ConfigFile = getFile();
    if (!ConfigFile.exists()) {
        ConfigFile.createNewFile();
    }

And this is the Info class:

package com.stm_wu_labor;

import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Merduma
 */
@XmlRootElement
public class Info {

    Info() {

    }

    public Info(String gender, int age, int SchoolClass) {
        this.gender = gender;
        this.age = age;
        this.SchoolClass = SchoolClass;
    }

    private String gender;

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSchoolClass() {
        return SchoolClass;
    }

    public void setSchoolClass(int SchoolClass) {
        this.SchoolClass = SchoolClass;
    }
    private int age;
    private int SchoolClass;
}

The Programm stops when i try to start it on Android please help me

Here is adb logcat(I hope this is relevant):   
I/System.out(13007): The programm started
I/System.out(13007): Succesfully started
I/System.out(13007): Info was added
W/art     (13007): Unresolved exception class when finding catch block: javax.xml.bind.PropertyException
W/art     (13007): Unresolved exception class when finding catch block: javax.xml.bind.JAXBException
W/System.err(13007): Exception in Application start method
I/System.out(13007): QuantumRenderer: shutdown
W/System.err(13007): java.lang.reflect.InvocationTargetException
W/System.err(13007):    at java.lang.reflect.Method.invoke(Native Method)
W/System.err(13007):    at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err(13007):    at javafxports.android.DalvikLauncher$1.run(DalvikLauncher.java:188)
W/System.err(13007):    at java.lang.Thread.run(Thread.java:818)
W/System.err(13007): Caused by: java.lang.RuntimeException: Exception in Application start method
W/System.err(13007):    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
W/System.err(13007):    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$137(LauncherImpl.java:182)
W/System.err(13007):    at com.sun.javafx.application.LauncherImpl.access$lambda$1(LauncherImpl.java)
W/System.err(13007):    at com.sun.javafx.application.LauncherImpl$$Lambda$2.run(Unknown Source)
W/System.err(13007):    ... 1 more
W/System.err(13007): Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/bind/JAXBContext;
W/System.err(13007):    at com.stm_wu_labor.STM_WU_LABOR.saveConfig(STM_WU_LABOR.java:161)
W/System.err(13007):    at com.stm_wu_labor.STM_WU_LABOR.start(STM_WU_LABOR.java:65)
W/System.err(13007):    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$144(LauncherImpl.java:863)
W/System.err(13007):    at com.sun.javafx.application.LauncherImpl.access$lambda$8(LauncherImpl.java)
W/System.err(13007):    at com.sun.javafx.application.LauncherImpl$$Lambda$9.run(Unknown Source)
W/System.err(13007):    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$157(PlatformImpl.java:326)
W/System.err(13007):    at com.sun.javafx.application.PlatformImpl.access$lambda$6(PlatformImpl.java)
W/System.err(13007):    at com.sun.javafx.application.PlatformImpl$$Lambda$7.run(Unknown Source)
W/System.err(13007):    at com.sun.javafx.application.PlatformImpl.lambda$null$155(PlatformImpl.java:295)
W/System.err(13007):    at com.sun.javafx.application.PlatformImpl.access$lambda$18(PlatformImpl.java)
W/System.err(13007):    at com.sun.javafx.application.PlatformImpl$$Lambda$19.run(Unknown Source)
W/System.err(13007):    at java.security.AccessController.doPrivileged(AccessController.java:52)
W/System.err(13007):    at com.sun.javafx.application.PlatformImpl.lambda$runLater$156(PlatformImpl.java:294)
W/System.err(13007):    at com.sun.javafx.application.PlatformImpl.access$lambda$5(PlatformImpl.java)
W/System.err(13007):    at com.sun.javafx.application.PlatformImpl$$Lambda$6.run(Unknown Source)
W/System.err(13007):    at com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:92)
W/System.err(13007):    at com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:51)
W/System.err(13007):    ... 1 more
W/System.err(13007): Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.xml.bind.JAXBContext" on path: DexPathList[[zip file "/data/app/com.stm_wu_labor-2/base.apk"],nativeLibraryDirectories=[/data/app/com.stm_wu_labor-2/lib/arm, /vendor/lib, /system/lib]]
W/System.err(13007):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
W/System.err(13007):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
W/System.err(13007):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
W/System.err(13007):    ... 18 more
W/System.err(13007):    Suppressed: java.lang.ClassNotFoundException: javax.xml.bind.JAXBContext
W/System.err(13007):            at java.lang.Class.classForName(Native Method)
W/System.err(13007):            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
W/System.err(13007):            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
W/System.err(13007):            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
W/System.err(13007):            ... 19 more
W/System.err(13007):    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

解决方案

When porting Java/JavaFX projects that work fine on Desktop to Android/iOS, and that make use of third party dependencies, you should be aware that not all those dependencies can be exported.

For instance, in terms of MySQL, it could be more convenient use SQLite. Check this post about using different SQLite libraries depending on the platform.

In the case of XML, it could be the same case. This question already mentions the drawback of using JAXB on Android, and proposes some alternatives.

Using Gluon Charm Down open source library (check this to add it to the build script) you can easily manage a local properties file in any platform, using the SettingService, that allows storing and retrieving properties:

SettingService settings = PlatformFactory.getPlatform().getSettingService();

settings.store("Info.Age", 10);
...
if (settings.retrieve("Info.Age") != null) {
    int age = Integer.parseInt(settings.retrieve("Info.Age"));
}

Finally, whenever you find any problem with your application on Android, use debugging with adb, to find out the possible exceptions.

adb is located under the Android sdk folder, sdk/platform-tools/.

Connect your mobile device to the USB port, and on command line, type:

./adb logcat -v threadtime

Launch the application, and find out the relevant parts of the messages in the console, possible exceptions or logging messages (using System.out.println or System.err.println).

这篇关于JavaFX的胶子保存XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆