1

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
Community
  • 1
  • 1
Aaron Stein
  • 526
  • 7
  • 18
  • Use `./adb logcat` on the command line with your device connected, you will find the exception and it will let you know what's wrong with your code. `adb` is located under Android/sdk/platform-tools. Post the relevant parts of the output if you can't find it. – José Pereda Dec 21 '15 at 09:52
  • I don't use avd so thx for your help. – Aaron Stein Dec 21 '15 at 16:25
  • You have android sdk installed, so you can use `adb`. Just connect your phone, enable USB debugging (under settings), and launch the app, you will see all the messages on the console, and that's how you will be able to find out what is happening. – José Pereda Dec 21 '15 at 17:05
  • AHHHH Ok thanks I'll do it tomorrow thanks. – Aaron Stein Dec 21 '15 at 19:46
  • I added in the question the relevant output when it's not relevant pls say what could be relevant. – Aaron Stein Dec 22 '15 at 10:30
  • That doesn't seem to be helpful. You have to look for the FXActivity ID, but before it's killed. You should find the exception. Adding System.out.println to your code will help as well. – José Pereda Dec 22 '15 at 10:33
  • I added another stacktrace maybe this is relevant – Aaron Stein Dec 22 '15 at 10:44
  • Is that related to your app? Notice that you will see all the output from your device... – José Pereda Dec 22 '15 at 10:59
  • I'm sorry now I found some relevant stacktrace ;D it seems that it don't found the classes I will post it. – Aaron Stein Dec 22 '15 at 11:00
  • Exception says it: `ClassNotFoundException: Didn't find class "javax.xml.bind.JAXBContext"`. That means JAXBContext dependencies are not added to your application, and Android doesn't include them. Check [this](http://stackoverflow.com/questions/5461127/using-jaxb-with-google-android) question, for instance. I'll suggest using other alternatives, like a properties file – José Pereda Dec 22 '15 at 11:06
  • Ok Thanks you could post an answer so that somebody who had the same problem know how to solve it. – Aaron Stein Dec 22 '15 at 11:07

1 Answers1

1

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).

Community
  • 1
  • 1
José Pereda
  • 44,311
  • 7
  • 104
  • 132