簡體   English   中英

Javafx Gluon保存XML文件

[英]Javafx Gluon save XML File

在我被問到如何立即獲取文件之前,我想在Android上保存xml文件;我被卡住了;(我使用Gluon(Netbeans插件,並且已經做了他所說的一切: JavaFXPorts(Gluon插件)在Android上保存文件

您能幫我我想在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");
}

這是我的稱呼:

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

這是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;
}

當我嘗試在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

將可在桌面上正常工作的Java / JavaFX項目移植到Android / iOS並利用第三方依賴項時,應注意並非可以導出所有這些依賴項。

例如,就MySQL而言,使用SQLite可能會更方便。 選中此職位有關使用取決於平台不同的SQLite庫。

對於XML,情況可能相同。 這個問題已經提到了在Android上使用JAXB的缺點,並提出了一些替代方案。

使用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"));
}

最后,每當在Android上發現應用程序有問題時,請使用adb進行調試,以找出可能的異常。

adb位於Android sdk文件夾sdk/platform-tools/

將您的移動設備連接到USB端口,然后在命令行上鍵入:

./adb logcat -v threadtime

啟動應用程序,並在控制台中找到消息的相關部分,可能的異常或記錄消息(使用System.out.printlnSystem.err.println )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM