0

我正在使用一个名为 Simple 的框架,它对序列化和 deser 非常有帮助。xml 内容。但是我无法将 xml 文件序列化为对象。输出对象应该填充信息,但我得到一个空指针异常。

这是我的java代码:

    InputStream is = getResources().openRawResource(R.xml.startingdata);
    Serializer ser = new Persister();
    Data data = ser.read(Data.class, is);

我在 res/xml/ 中有一个名为startingdata.xml 的文件。我正在尝试将数据序列化为“数据”对象。但是,我得到了一个与数据对象相关的 NullPointerException。

这是startingdata.xml:

<data>
<categories>
    <category>Inbox</category>
    <category>Private</category>
    <category>Work</category>
    <category>Business</category>
</categories>

<todos>
    <todo>
        <id>1</id>
        <text>Explore the app!</text>
    </todo>

    <todo>
        <id>2</id>
        <text>Add more todos!</text>
        <date>2013-05-09 12:21:55 CET</date>
    </todo>
</todos>

xml模型类我觉得还可以,没有问题,但是如果你需要看的话我可以贴出来。

以下是模型类:

数据.java

@Root
public class Data {

 @ElementList
 public List<Category> categories;

 @ElementList
 public List<ToDo> todos;

}

@Element(name="todo")
 public class ToDo {

@Element(required=true)
public String id;

@Element(required=true)
public String text;

@Element(required=false)
public Date date;

 }

@Element(name="category")
public class Category {

    @Element(required=true)
    public String text;

}

我正在使用 Toast 检查创建的数据对象是否已创建。

Toast toast = Toast.makeText(getApplicationContext(), data.toString(), Toast.LENGTH_LONG);
        toast.show();

这是完整的堆栈跟踪:

05-09 14:13:38.407: E/AndroidRuntime(9246): FATAL EXCEPTION: main
05-09 14:13:38.407: E/AndroidRuntime(9246): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.todo.wanttodo/com.todo.wanttodo.MainActivity}: java.lang.NullPointerException
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.os.Looper.loop(Looper.java:137)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at java.lang.reflect.Method.invoke(Method.java:511)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at dalvik.system.NativeStart.main(Native     Method)
05-09 14:13:38.407: E/AndroidRuntime(9246): Caused by: java.lang.NullPointerException
05-09 14:13:38.407: E/AndroidRuntime(9246):     at com.todo.wanttodo.MainActivity.onCreate(MainActivity.java:77)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.Activity.performCreate(Activity.java:5104)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-09 14:13:38.407: E/AndroidRuntime(9246):     at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-09 14:13:38.407: E/AndroidRuntime(9246):     ... 11 more

先感谢您!

4

2 回答 2

0

您确定startingdata.xml 文件吗?

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <categories>
        <category>Inbox</category>
        <category>Private</category>
        <category>Work</category>
        <category>Business</category>
    </categories>

    <todos>
        <todo>
            <id>1</id>
            <text>Explore the app!</text>
        </todo>

        <todo>
            <id>2</id>
            <text>Add more todos!</text>
           <date>2013-05-09 12:21:55 CET</date>
       </todo>
    </todos>
</data>

您可以使用 XmlPullParser 尝试不同的方式:android - trainingandroid - reference

于 2013-05-09T12:07:42.913 回答
0

在我的项目中,我在 DAO 类中设置了 @Default。

@Root(name="data")
@Default(value = DefaultType.FIELD, required = false)
public class Data {

   @ElementList
   public List<Category> categories;

   @ElementList
   public List<ToDo> todos;

    ....

}
于 2013-05-09T13:12:09.640 回答