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


}
4

1 回答 1

1

Kailash,您不能只是将 Java 代码粘贴到 Android 活动中并期望它能够工作。很可能您将该程序作为 Java 应用程序运行,因此它惨遭失败。

您不能static void main(String[] args)在 Android 应用程序中使用 a,因此第一步将是修改该方法的签名(remove static、removeString[]参数、make it private)。

之后,您的 Activity 根本不会做任何事情,因为您的方法不会从任何地方被调用。您必须找到一个地方(可能onCreate())来开始AsyncTask解析 XML(此外,您必须切换到通过 ID 检索资源的 Android 方式,因为您无法从任何 Android 设备真正访问您的工作区。. .)

于 2012-11-01T11:06:05.440 回答