Android 解析 XML 文件时出错

发布于 2024-10-28 09:18:25 字数 1951 浏览 1 评论 0原文

在我的应用程序中,我尝试解析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2024-11-04 09:18:25

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

您还需要调用 Log.e 方法如下:

    Log.e("yourClass", "unexpected exception", 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:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文