我试图在eclipse上运行这个程序,但在控制台中得到这个错误

时间:2012-11-01 07:00:33

标签: 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
#
  

作为Android应用程序运行。我是JAVA和android的新手。并且无法运行此代码,因为控制台中显示以下错误。所以请任何人都可以帮助我。在这段代码中,我试图解析本地XML文件中的一些数据并将其写入新文件中。

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;
}


}

1 个答案:

答案 0 :(得分:1)

Kailash,您不能只是将Java代码粘贴到Android活动中并期望它能够正常工作。可能你将这个程序作为Java应用程序运行,所以很难失败。

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

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