java.lang.RuntimeException:无法启动活动ComponentInfo java.lang.NullPointerException

java.lang.RuntimeException:无法启动活动ComponentInfo java.lang.NullPointerException,java,android,Java,Android,我创建了一个应用程序,使用SAXParser解析XML文件,代码如下 下面是我主要活动的代码 public class XMLAppActivity extends Activity{ ArrayList<Question> questionList = new ArrayList<Question>(); TextView tvXmlReader; InputSource xmlSource; InputStream xmlIs; Stri

我创建了一个应用程序,使用SAXParser解析XML文件,代码如下

下面是我主要活动的代码

public class XMLAppActivity extends Activity{
 ArrayList<Question> questionList = new ArrayList<Question>();          
 TextView tvXmlReader;
 InputSource xmlSource;
 InputStream xmlIs;
 String xmlFile;


  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      tvXmlReader=(TextView)findViewById(R.id.my_xml);

      try 
        {
          xmlIs= getAssets().open("surveyquestions.xml");
          xmlFile = convertStreamToString(xmlIs);
        } 
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } 

     xmlSource=  new InputSource(new StringReader(xmlFile));
      tvXmlReader.setText(parseXML().toString());
  }

 private String parseXML() {

        try {


            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            QuestionXMLHandler myXMLHandler = new QuestionXMLHandler();
            xr.setContentHandler(myXMLHandler);

            xr.parse(xmlSource);

            questionList= myXMLHandler.getQuestionList();
            return "Success";

        }
        catch (Exception e) {
          return "Failure due to " + e.toString();
        }
   }

 public  String convertStreamToString(InputStream is)
            throws IOException {
            Writer writer = new StringWriter();

            char[] buffer = new char[2048];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            String text = writer.toString();
            return text;
    }
}
试试这个

xmlIs= this.getResources().getAssets().open("surveyquestions.xml");
而不是

xmlIs= getAssets().open("surveyquestions.xml");
我认为“
问题l
”没有在您的代码中初始化

在以下代码中使用

 @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
          if (qName.equalsIgnoreCase("Question")) {
              questionL.add(cQuestion);
            }
            if (qName.equalsIgnoreCase("QuestionText")) {
                cQuestion.setQuestionText(cValue);
            }

            if(qName.equalsIgnoreCase("Answer")){
                cQuestion.getAnswers().add(cValue);
            }

    }
我发现了我的错误!!它位于我的XML的End元素方法中 处理程序类。基本上,下面这行有一些问题。 if(qName.equalsIgnoreCase(“应答”)){ cQuestion.getAnswers().add(cValue);}


为我工作

你应该少放代码多放日志!!错误来自哪里?是什么原因造成的?放上完整的logcat,logcat有类名和行号。也看到我现在已经添加了完整的logcat谢谢大家的帮助!!我发现了我的错误!!它位于我的XML处理程序类的End元素方法中。基本上,下面这行有一些问题。如果(qName.equalsIgnoreCase(“Answer”){cQuestion.getAnswers().add(cValue);},那么我删除了它。但这给了我另一个问题,那就是在我的问题类中,我有一个答案列表,你可以在答案标签下看到我的xml,我必须在我的类中添加所有这些答案,所以我如何实现它,有什么想法吗?我能够阅读我测试过的xml文件,当我尝试使用SAX解析文件时,问题出现了。我已经看到@Dheeresh的答案,但仍然遇到了相同的问题。我已经发布了我的完整日志now@AdityaPratap:代码中的行号QuestionXMLHandler.java:52是什么?Imran Khan:-thnx buddy fr ur help,QuestionXMLHandler.java:52基本上,它是处理程序类if(localName.equalsIgnoreCase(“Answer”){cQuestion.getAnswers().add(cValue);}的结束元素方法。我评论说部件和程序现在运行良好。但这又引出了另一个问题。正如您在我的XMl内部问题标签中看到的,我有答案标签,内部答案标签中有多个答案,所以我想做的是将所有这些答案添加到我的问题类答案列表成员中。但不知为什么,逻辑失败了,你能建议其他方法吗?我已经初始化了它并更新了上面的代码,但它仍然给出了相同的错误,我已经添加了完整的日志猫!!你能试试这个私有的ArrayList问题吗;问题cQuestion=新cQuestion();对于代码中的测试……。@Dheerash:我发现了问题,thnx,它在我的处理程序类的End-Element方法中,在这一行if(qName.equalsIgnoreCase(“Answer”){cQuestion.getAnswers().add(cValue);},正如你从上面可以看到的,我只是跟踪了LogCat并发现了错误,顺便说一句,问题只出在cQuestion身上,所以伙计,你救了我
xmlIs= this.getResources().getAssets().open("surveyquestions.xml");
xmlIs= getAssets().open("surveyquestions.xml");
 @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
          if (qName.equalsIgnoreCase("Question")) {
              questionL.add(cQuestion);
            }
            if (qName.equalsIgnoreCase("QuestionText")) {
                cQuestion.setQuestionText(cValue);
            }

            if(qName.equalsIgnoreCase("Answer")){
                cQuestion.getAnswers().add(cValue);
            }

    }