在Android中解析XML文件时出错

时间:2022-12-01 13:11:30

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.

在我的应用程序中,我试图解析XML文件。我能够解析它,在logcat中我可以找到标签的数量但在我的文本视图中我无法查看它。现在我尝试在文本视图中只打印一个名为question的标签。我的文本视图名为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

在我的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

The NullPointerException is been shown from the outer catch block.

NullPointerException已从外部catch块显示。

1 个解决方案

#1


0  

Try running logcat with -v long. That should print out all available metadata ... and hopefully the full stack trace.

尝试使用-v long运行logcat。这应该打印出所有可用的元数据......并希望完整的堆栈跟踪。

You also need to call the three argument version of Log.e method something like this:

您还需要调用Log.e方法的三个参数版本,如下所示:

    Log.e("yourClass", "unexpected exception", e);

What you are currently doing is logging e.toString() which doesn't include the full stacktrace.

你目前正在做的是记录e.toString(),它不包括完整的堆栈跟踪。

#1


0  

Try running logcat with -v long. That should print out all available metadata ... and hopefully the full stack trace.

尝试使用-v long运行logcat。这应该打印出所有可用的元数据......并希望完整的堆栈跟踪。

You also need to call the three argument version of Log.e method something like this:

您还需要调用Log.e方法的三个参数版本,如下所示:

    Log.e("yourClass", "unexpected exception", e);

What you are currently doing is logging e.toString() which doesn't include the full stacktrace.

你目前正在做的是记录e.toString(),它不包括完整的堆栈跟踪。