Android-studio-XML序列化--出现错误--尚未解决

1.XML序列化
序列化是将对象状态转换为可保持或传输的过程。在序列化对象时,需要使用XmlSerialize序列化器,它可以将IO流中传输的对象变得像基本类
型数据一样,实现数据传递的功能。
–序列化后的对象以XML形式保存

2.案例:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="序列化XML文件"
        android:onClick="serializer"/>

</RelativeLayout>
MainActivity.java
package com.example.zlf.a52;

import android.app.Activity;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.Toast;

import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private List<Person> userData; //1.首先创建了一个保存数据的集合

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 2.创建了集合的对象,模拟假数据
        userData = new ArrayList<Person>();
        for (int i = 0; i < 3; i++) {
            userData.add(new Person("王" + i, 100 - i, 80 - i));
        }
    }

    public void serializer(View view) {

        try {
            //3.创建一个XmlSerializer的对象
            XmlSerializer serializer = Xml.newSerializer();
            //4将文件保存在SD卡中,因此,需要创建一个file
            File file = new File(Environment.getExternalStorageDirectory(),
                    "person.xml");
            // 5需要设置文件的编码方式-输出流
            FileOutputStream os = new FileOutputStream(file);

            serializer.setOutput(os, "utf-8");
            //写入XML文件标志
            serializer.startDocument("utf-8", true);
            //开始节点
            serializer.startTag(null, "persons");
            int count = 0;

            for (Person person : userData) {
                serializer.startTag(null, "person");
                serializer.attribute(null, "id", count + "");

                serializer.startTag(null, "name");
                serializer.text(person.getName());
                serializer.endTag(null, "name");

                serializer.startTag(null, "age");
                serializer.text(person.getAge()+"");
                serializer.endTag(null, "age");

                serializer.startTag(null, "score");
                serializer.text(String.valueOf(person.getScore()));
                serializer.endTag(null, "score");

                serializer.endTag(null, "person");

                count++;
            }

            serializer.endTag(null, "persons");
            serializer.endDocument();
            os.close();
            Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
        }
    }
}

Person.java

package com.example.zlf.demo52_xml;

import android.content.Intent;

class Person {
    private String name;
    private  Integer age;
    private Integer score;
    public Person(String name, Integer age, Integer score) {
        super();
        this.name=name;
        this.age=age;
        this.score=score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public Integer getScore() {
        return score;
    }
 public String toString() {
        return "Person [name=" + name + ", age=" + age + ", score=" + score
                + "]";
    }
}

运行失败,出现的错误:

2018-05-03 09:57:14.858 6503-6503/? I/art: Not late-enabling -Xcheck:jni (already on)
2018-05-03 09:57:14.858 6503-6503/? W/art: Unexpected CPU variant for X86 using defaults: x86
2018-05-03 09:57:15.054 6503-6503/com.example.zlf.a52 W/System: ClassLoader referenced unknown path: /data/app/com.example.zlf.a52-1/lib/x86
2018-05-03 09:57:15.064 6503-6503/com.example.zlf.a52 I/InstantRun: starting instant run server: is main process
2018-05-03 09:57:15.258 6503-6521/com.example.zlf.a52 I/OpenGLRenderer: Initialized EGL, version 1.4
2018-05-03 09:57:15.258 6503-6521/com.example.zlf.a52 D/OpenGLRenderer: Swap behavior 1
2018-05-03 09:57:15.258 6503-6521/com.example.zlf.a52 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2018-05-03 09:57:15.258 6503-6521/com.example.zlf.a52 D/OpenGLRenderer: Swap behavior 0
2018-05-03 09:57:15.325 6503-6521/com.example.zlf.a52 D/EGL_emulation: eglCreateContext: 0xa8b654c0: maj 2 min 0 rcv 2
2018-05-03 09:57:15.367 6503-6521/com.example.zlf.a52 D/EGL_emulation: eglMakeCurrent: 0xa8b654c0: ver 2 0 (tinfo 0x9eee35f0)

2018-05-03 09:57:15.393 6503-6521/com.example.zlf.a52 D/EGL_emulation: eglMakeCurrent: 0xa8b654c0: ver 2 0 (tinfo 0x9eee35f0)

除此之外虚拟机:

10:11 Emulator: audio: Failed to create voice `goldfish_audio'


10:11 Emulator: qemu-system-i386.exe: warning: opening audio output failed


10:11 Emulator: audio: Failed to create voice `goldfish_audio_in'


10:11 Emulator: qemu-system-i386.exe: warning: opening audio input failed


10:11 Emulator: audio: Failed to create voice `dac'


10:11 Emulator: audio: Failed to create voice `adc'


10:11 Emulator: audio: Failed to create voice `dac'


10:11 Emulator: audio: Failed to create voice `adc'


10:11 Emulator: getGeneralInfo: unrecoverable failure

写不出那个person.xml 文件,出错原因,没查到。



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值