java - 我试图在 eclipse 上运行这个程序,但在控制台中出现这个错误

标签 java android xml eclipse parsing

Invalid layout of java.lang.String at value
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (javaClasses.cpp:129), pid=6223, tid=3064822640
#  fatal error: Invalid layout of preloaded class
#
# JRE version: 7.0_07-b10
# Java VM: Java HotSpot(TM) Server VM (23.3-b01 mixed mode linux-x86 )
# Failed to write core dump. Core dumps have been disabled. To enable core dumping,     try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/kailash/workspace/Parsing/hs_err_pid6223.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
#

Running as Android application. I am new to JAVA and android. and not able to run this code as the following error is shown in console. so please anybody can help me with this. Here in this code i'm trying to parse some data from a local XML file and write it in a new file.

package com.demo.parsing;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jsoup.Jsoup;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity{

    //private static OutputStreamWriter out;


    public static void main(String[] args) throws FileNotFoundException {
        FileInputStream pustaka = new FileInputStream("/home/kailash/workspace/parsing/pustaka-feed.xml");

        try {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            Document feed = builder.parse(pustaka);

            NodeList items = feed.getElementsByTagName("item");

            List<String> urls = new LinkedList<String>();

            for (int i = 0; i < items.getLength(); i++) {
                Element item = (Element) items.item(i);
                Element description = (Element) item.getElementsByTagName(
                        "description").item(0);
                urls.addAll(getImages(builder, description.getTextContent()));
            }

             FileWriter f = new FileWriter(new File("outfile"));

            for (String url : urls) {
                f.write(url + "\n");
            }

            f.close();

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static List<String> getImages(DocumentBuilder builder, String content) throws SAXException, IOException {
        List<String> urls = new LinkedList<String>();

        for (org.jsoup.nodes.Element img : Jsoup.parse(content).getElementsByTag("img")) {
            urls.add(img.attributes().get("src"));
        }

        return urls;
    }



public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}




public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


}

最佳答案

Kailash,您不能只是将 Java 代码粘贴到 Android Activity 中并期望它能够运行。很可能您将此程序作为 Java 应用程序运行,因此它不幸地失败了。

您不能在 Android 应用程序中使用 static void main(String[] args),因此第一步是修改该方法的签名(删除 static,删除 String[] 参数,使其成为 private)。

在那之后,您的 Activity 将不会执行任何操作,因为您的方法不会从任何地方调用。您必须找到一个地方(可能是 onCreate())来启动 AsyncTask 来解析 XML(此外,您还必须切换到 Android 检索资源的方式通过 ID,因为您无法真正从任何 Android 设备访问您的工作区...)

关于java - 我试图在 eclipse 上运行这个程序,但在控制台中出现这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13172415/

相关文章:

java - 事务管理器不承担事务

java - 我必须在 server.xml 中编辑什么?

php - XML 到数据库 - foreach 循环到 MySQL

java - 使用 java 的 JSON 模式验证

java - 如何使用 Scanner 处理无效输入(InputMismatchException)引起的无限循环

java - 如何设置 x 按钮一次清除文本,第二次关闭 SearchView Android?

android - 获取 Listview 默认点击颜色,具体取决于设备

javascript - 如何在网站中进行深层链接

Android简单XML解析

javascript - 具有来自 responseXML 的命名空间的跨浏览器 'getElementsByTagName'