写Android时总是遇到空指针问题,主要的原因是因为好多时候类没有初始化的缘故。
public class XMLParserHandler extends DefaultHandler {
private Book book=new Book();
private ArrayList<Book> booklist=new ArrayList<Book>();
private String nodeName;
private StringBuilder value;
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
value=new StringBuilder();
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
nodeName=localName;
value.setLength(0);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if(qName.equals("data")){
booklist.add(book);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
value.append(ch,start,length);
Log.d("mainactivity",value.toString().trim());
if(nodeName.equals("id")){
book.setId(Integer.parseInt(value.toString().trim()));
}else if(nodeName.equals("study_number")){
book.setStudy_number(Integer.parseInt(value.toString().trim()));
}else if(nodeName.equals("name")){
book.setName(value.toString().trim());
}else if(nodeName.equals("sex")){
book.setSex(value.toString().trim());
}else if(nodeName.equals("major")){
book.setMajor(value.toString().trim());
}else if(nodeName.equals("enroll_year")){
book.setEnroll_year(Integer.parseInt(value.toString().trim()));
}else if(nodeName.equals("class")){
book.setClassw(value.toString().trim());
}else if(nodeName.equals("telephone")){
book.setTelephone(value.toString().trim());
}else if(nodeName.equals("qq")){
book.setQq(value.toString().trim());
}else if(nodeName.equals("email")){
book.setEmail(value.toString().trim());
}else if(nodeName.equals("remark")){
book.setRemark(value.toString().trim());
}
}
public ArrayList<Book> getBooklist() {
return booklist;
}
public void setBooklist(ArrayList<Book> booklist) {
this.booklist = booklist;
}
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
几个日常问题的积累
日常工作中经常会碰到一些小问题,有的是知道了,就是知道了,有的是得研究出根本原
运维 linux shell vmware docker -
[转]Android 开发中的日常积累
id最佳性能实践(1):合理管理内存Android最佳性能实践计插件,可用于统计是否达到65536个方法Android-Drawa
移动开发 json 后端 Android android