我正在解析XML文件并将XML值分配给类变量。这里是主要的活动和变量类:
package com.example.questions;
public class Test extends Activity
{
private TextView question;
private RadioButton rdooption1;
private RadioButton rdooption2;
private RadioButton rdooption3;
private RadioButton rdooption4;
private static final String FILENAME = "xmlFileName.xml";
private FileInputStream fin;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question_layout);
}
@SuppressWarnings("null")
private void parseXML(XmlPullParser xpp) throws XmlPullParserException, IOException
{
List<QuestionArrayList> questions = null;
QuestionArrayList currentquestion = null;
while(xpp.next()!=XmlPullParser.END_DOCUMENT )
{
questions= new ArrayList<QuestionArrayList>();
if (xpp.getEventType() != XmlPullParser.START_TAG)
{
continue;
}
String name = xpp.getName();
if(name.equals("Question"))
{
//String questionmy = xpp.nextText();
//here error is giving
currentquestion.question_name=xpp.nextText();
}
questions.add(currentquestion);
System.out.println("Size of al after additions: " + questions.size());
System.out.println("Contents of al: " + questions);
}
}
}
这是变量类:
public class QuestionArrayList
{
public int question_set_id;
public int question_id;
public String question_type;
public String question_name;
public String option_one;
public String option_two;
public String option_three;
public String option_four;
}
答案 0 :(得分:3)
初始化您的QuestionArrayList
课程。你刚刚为它声明了变量。
currentquestion = new QuestionArrayList():
答案 1 :(得分:0)
while(xpp.next()!=XmlPullParser.END_DOCUMENT )
{
questions= new ArrayList<QuestionArrayList>();
currentquestion = new QuestionArrayList():
//在这里初始化cuurrentqeustion。
if (xpp.getEventType() != XmlPullParser.START_TAG)
{
continue;
}
String name = xpp.getName();
if(name.equals("Question"))
{
//String questionmy = xpp.nextText();
//here error is giving
currentquestion.question_name=xpp.nextText();
questions.add(currentquestion);
}
//as per your condition it will add blank currentquestion in question array list..
//its bettter put it question.add(currentquestion) inside if condition.
//questions.add(currentquestion);
System.out.println("Size of al after additions: " + questions.size());
System.out.println("Contents of al: " + questions);
}
}