简体   繁体   中英

I'm trying to Run this program on eclipse but get this error in console

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, you can't just paste your Java code in an Android activity and expect it to work. Likely you run this program as a Java application, so it miserably fails.

You can't use a static void main(String[] args) in an Android application, so the first step will be to modify that signature of that method (remove static , remove the String[] argument, make it private ).

After that, your Activity simply won't do anything because your method will not be called from anywhere. You have to find a place (likely onCreate() ) to start an AsyncTask to parse the XML (also, you'll have to switch to the Android way of retrieving resources by ID, because you can't really access your workspace from any Android device...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM