1

我正在开发一个 android 应用程序,并希望使用 xmlSerializer 创建一些数据并将其存储到一个 xml 文件中,并且该文件应该保存在 android 内部存储中,....这是我不知道它有什么问题,当我运行应用程序它会给我一个错误“不幸的是项目已停止”..请帮助我!

package com.example.xmlnizam;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;



import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.util.Xml;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String Filename = "xmlfile.xml"; 
        FileOutputStream fos = null; 
        try 
        {
            fos = openFileOutput(Filename, Context.MODE_PRIVATE);  
            //File path = getFileStreamPath(Filename);  
            //Toast.makeText(MainActivity.this, "File has been created", Toast.LENGTH_LONG).show();     
        } 
        catch (FileNotFoundException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "Error Creating FIle", Toast.LENGTH_LONG).show(); 
        }

        //XmlSerializer to write into Xml file 
        XmlSerializer xmlSerializer = Xml.newSerializer();  
        try
        {
            xmlSerializer.setOutput(fos, "UTF-8");
            xmlSerializer.startDocument(null, Boolean.valueOf(true));
            xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
            xmlSerializer.startTag("", "AllData");
                xmlSerializer.startTag("", "Person");
                xmlSerializer.attribute(null, "id", "1");
                    xmlSerializer.startTag("", "Name");
                    xmlSerializer.text("nomi");
                    xmlSerializer.endTag(null, "Name");

                    xmlSerializer.startTag("", "Age");
                    xmlSerializer.text("21");
                    xmlSerializer.endTag(null, "Name");

                    xmlSerializer.startTag("", "Sex");
                    xmlSerializer.text("male");
                    xmlSerializer.endTag(null, "Name");
                xmlSerializer.endTag(null, "Person");
            xmlSerializer.endTag(null, "AllData");
            xmlSerializer.endDocument();
            xmlSerializer.flush(); 
            fos.close(); 
            Toast.makeText(MainActivity.this, "Successfully Inserted", Toast.LENGTH_LONG).show(); 
        }
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "Error Inserting", Toast.LENGTH_LONG).show(); 
        } 


    }
}
4

0 回答 0