0

我有一个问题,我认为是一个概念错误

我创建了一个 java 类,当调用构造函数时会创建另一个自定义对象

第一次使用参数 start == true 运行代码并且

questions  = new ArrayList<Question>();
            questions.add(this.question);

然后调用 writeXML 并正常工作

但如果 start != true 则首先读取 xml 的内容并存储在自定义问题对象的数组列表中

questions = readXML(ctx);
questions.add(this.question);
readXML return a arraylist of questions objects from readed xml

问题是当代码到达时调用 writexml 方法发送 arraylist 作为参数

 for( Question q : questions ){


                serializer.startTag(null, "question");
                serializer.attribute(null,"grupo" , q.getGroup());
                serializer.attribute(null,"question" , q.getQuestion());
                serializer.text(q.getAnswer());
                serializer.endTag(null, "question");
            }

当代码尝试执行 serializer.attribute(null,"grupo" , q.getGroup()); q.getGroup() 失败并以空消息捕获异常

我做错了什么?

非常感谢

这是完整的课程

package...
inports...

class AsyncLoadXML extends AsyncTask<Boolean, Integer, String> {

    private Context ctx;    
    private List<Question> questions;
    private Question question;
    String TAG;

    public AsyncLoadXML(Context c, String g, String p, String r) {
        this.ctx = c;
        question =new Question();
        question.setGroup(g);
        question.setQuestion(p);
        question.setAnswer(r);

    }

    @Override
    protected String doInBackground(Boolean... start) {

        if(!start[0]){
            try { questions = readXML(ctx);
            } 
            catch (IOException e) { e.printStackTrace(); }
        }

        else {questions  = new ArrayList<Question>();
        questions.add(this.question);
        }

        try { writeXML(ctx, questions); } 
        catch (IOException e) { e.printStackTrace(); }


        return "done";
    }
    protected void onPostExecute(String result) {
        if(!result.equals("")) 
            Toast.makeText(this.ctx, result, Toast.LENGTH_LONG).show();
    }



    public String writeXML(Context ctx, List<Question> questions) throws IOException{   
        FileOutputStream fout = null;

        try {
            fout = ctx.openFileOutput("answers.xml", Context.MODE_PRIVATE);
        } catch (FileNotFoundException e) {
            return e.getMessage();

        }

        XmlSerializer serializer = Xml.newSerializer();
        try {
            serializer.setOutput(fout, "UTF-8");
            serializer.startDocument(null, true);
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

            serializer.startTag(null, "questions");


            for( Question q : questions ){


                serializer.startTag(null, "question");
                serializer.attribute(null,"grupo" , q.getGroup());
                serializer.attribute(null,"question" , q.getQuestion());
                serializer.text(q.getAnswer());
                serializer.endTag(null, "question");
            }

            serializer.endTag(null, "questions");


            serializer.endDocument();
            serializer.flush();
            fout.close();
            return "done";

        } catch (Exception e) {
            return e.getMessage();

        }
    }





    public List<Question> readXML(Context ctx) throws IOException{  
        FileInputStream fin = null;
        questions= new ArrayList<Question>();
        try {
            fin = ctx.openFileInput("answers.xml");
        } catch (Exception e) {

        }

        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(fin, "UTF-8");

            int event = parser.next();
            while(event != XmlPullParser.END_DOCUMENT) {

                if(event == XmlPullParser.START_TAG) {


                    question = new Question();

                    for(int i = 0; i < parser.getAttributeCount(); i++) {

                        if(parser.getAttributeName(i) == "group") question.setGroup(parser.getAttributeValue(i));
                        else if(parser.getAttributeName(i) == "question") question.setQuestion(parser.getAttributeValue(i));

                    }

                }

                if(event == XmlPullParser.TEXT && parser.getText().trim().length() != 0)
                {question.setAnswer(parser.getText());

                questions.add(question);
                }


                if((event == XmlPullParser.END_TAG))
                {
                    Log.d(TAG, "</"+ parser.getName() + ">");}


                event = parser.next();
            }
            fin.close();


        } catch (Exception e) {

            e.printStackTrace();
        }
        return questions;
    }

}

这是一个对象类

public class Question {

    private String group;
    private String question;
    private String answer;


    public Question(){

    }
    public Question(String g, String p, String r){
    group = g;
    question = p;
    answer = r;
    }

    public String getGroup(){
        return group;
    }

    public String getQuestion(){
        return question;
    }

    public String getAnswer(){
        return answer;
    }

    public void setGroup(String g){
        group = g;
    }

    public void setQuestion(String p){
        question = p;
    }

    public void setAnswer(String r){
        answer = r;
    }
}
4

0 回答 0