简体   繁体   English

Javafx Gluon保存XML文件

[英]Javafx Gluon save XML File

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 在我被问到如何立即获取文件之前,我想在Android上保存xml文件;我被卡住了;(我使用Gluon(Netbeans插件,并且已经做了他所说的一切: JavaFXPorts(Gluon插件)在Android上保存文件

Can you help me I want to save a XML File on Android, this is my Code yet: 您能帮我我想在Android上保存XML文件吗,这是我的代码吗:

    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: 这是Info类:

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 当我尝试在Android上启动该程序时,请停止运行

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. 将可在桌面上正常工作的Java / JavaFX项目移植到Android / iOS并利用第三方依赖项时,应注意并非可以导出所有这些依赖项。

For instance, in terms of MySQL, it could be more convenient use SQLite. 例如,就MySQL而言,使用SQLite可能会更方便。 Check this post about using different SQLite libraries depending on the platform. 选中此职位有关使用取决于平台不同的SQLite库。

In the case of XML, it could be the same case. 对于XML,情况可能相同。 This question already mentions the drawback of using JAXB on Android, and proposes some alternatives. 这个问题已经提到了在Android上使用JAXB的缺点,并提出了一些替代方案。

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: 使用Gluon Charm Down开放源代码库(选中此选项将其添加到构建脚本中),您可以使用SettingService轻松管理任何平台上的本地属性文件,该服务允许存储和检索属性:

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. 最后,每当在Android上发现应用程序有问题时,请使用adb进行调试,以找出可能的异常。

adb is located under the Android sdk folder, sdk/platform-tools/ . adb位于Android sdk文件夹sdk/platform-tools/

Connect your mobile device to the USB port, and on command line, type: 将您的移动设备连接到USB端口,然后在命令行上键入:

./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 ). 启动应用程序,并在控制台中找到消息的相关部分,可能的异常或记录消息(使用System.out.printlnSystem.err.println )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM