Android 解析 XML 文件时出错
在我的应用程序中,我尝试解析 XML 文件。我能够解析它,并且在 logcat 中我可以找到标签的数量,但在我的文本视图中我无法查看它。现在我试图在文本视图中仅打印一个名为 Question 的标签。我的文本视图名为 flip
。
以下是我的代码的一部分:
//XML parsing happens here
try
{
saxparserfactory1 = SAXParserFactory.newInstance();
saxparser1 = saxparserfactory1.newSAXParser();
xmlreader1 = saxparser1.getXMLReader();
inputstream1 = this.getResources().openRawResource(R.raw.worldhistory);
xmlreader1.setContentHandler(myXMLHandler);
xmlreader1.parse(new InputSource(inputstream1));
//getting the values of xml
flashcards = myXMLHandler.getflashcards();
flashcard = myXMLHandler.getflashcard();
try
{
o = flashcards.getFlashcard().size();
Log.e("Parsing", "flashcard size = "+o);
}
catch (Exception e)
{
Log.e("parsing",""+e);
}
String question ="" ;
if(index1 < flashcards.getFlashcard().size())
{
question = flashcards.getFlashcard().get(q[index1]).getQuestion();
Log.e("", "Qustion "+question);
Flip1.setText(question);
index1++;
}
}
catch(Exception e)
{
Log.e("",""+e);
}
}
在我的 logcat 中,我可以查看以下几行
03-31 19:25:16.578: ERROR/MyXMLHandler(10361): Flashcard created
03-31 19:25:16.578: ERROR/Parsing(10361): flashcard size = 68
03-31 19:25:16.578: ERROR/(10361): java.lang.NullPointerException
NullPointerException 从外部 catch 块中显示。
In my app I am trying to parse an XML file. I am able to parse it and in logcat I can find the number of tags but in my text view I am not able to view it. Now I am trying to print only one tag named as question in my text view. My text view is named flip
.
Following is one part of my code:
//XML parsing happens here
try
{
saxparserfactory1 = SAXParserFactory.newInstance();
saxparser1 = saxparserfactory1.newSAXParser();
xmlreader1 = saxparser1.getXMLReader();
inputstream1 = this.getResources().openRawResource(R.raw.worldhistory);
xmlreader1.setContentHandler(myXMLHandler);
xmlreader1.parse(new InputSource(inputstream1));
//getting the values of xml
flashcards = myXMLHandler.getflashcards();
flashcard = myXMLHandler.getflashcard();
try
{
o = flashcards.getFlashcard().size();
Log.e("Parsing", "flashcard size = "+o);
}
catch (Exception e)
{
Log.e("parsing",""+e);
}
String question ="" ;
if(index1 < flashcards.getFlashcard().size())
{
question = flashcards.getFlashcard().get(q[index1]).getQuestion();
Log.e("", "Qustion "+question);
Flip1.setText(question);
index1++;
}
}
catch(Exception e)
{
Log.e("",""+e);
}
}
In my logcat I am able to view the following lines
03-31 19:25:16.578: ERROR/MyXMLHandler(10361): Flashcard created
03-31 19:25:16.578: ERROR/Parsing(10361): flashcard size = 68
03-31 19:25:16.578: ERROR/(10361): java.lang.NullPointerException
The NullPointerException is been shown from the outer catch block.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
-v long
运行logcat
。这应该打印出所有可用的元数据......并希望打印出完整的堆栈跟踪。您还需要调用
Log.e
方法如下:您当前正在执行的操作是记录
e.toString()< /code> 不包含完整的堆栈跟踪。
Try running
logcat
with-v long
. That should print out all available metadata ... and hopefully the full stack trace.You also need to call the three argument version of
Log.e
method something like this:What you are currently doing is logging
e.toString()
which doesn't include the full stacktrace.