1

我想在 Android 上保存一个 xml 文件,然后我问了一个问题,现在我被卡住了如何获取文件;(我使用 Gluon(Netbeans 插件并且已经按照他所说的做了一切: JavaFXPorts(Gluon Plugin)在 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();
    }

这是信息类:

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
4

1 回答 1

1

将在桌面上运行良好的 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)。

于 2015-12-22T11:25:02.053 回答