我正在使用一個名爲Simple的框架,這對於序列化和deser很有幫助。 XML內容。但是我無法序列化一個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模型類都ok,而且是沒有問題的他們,但是如果你需要看到他們,我可以張貼。
這裏是模型類:
Data.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.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
預先感謝您!
堆棧跟蹤和模型類將非常有幫助 – 2013-05-09 12:03:47
我添加了所需的信息。感謝您的評論! – 2013-05-09 12:15:53
http://stackoverflow.com/questions/4488654/using-simple-xml-serialization-to-load-data-from-res-raw-on-android?rq=1 – 2013-05-09 12:19:32