XMLparser - 遗憾的是应用程序停止工作

时间:2013-04-11 11:03:11

标签: android xml parsing

我运行我的Android应用程序时遇到问题(v 4.0.3)我的应用程序停止工作。它基本上是XML解析应用程序

这里是主要活动

    package cz.jiri.podlipny.weather_design;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //new Stahni().execute();

    }


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

private class Stahni extends AsyncTask<Void, Void, ParserXMLObject>{

        @Override
        protected ParserXMLObject doInBackground(Void... params){
            ParserXMLObject XMLdata = null;
            try {
                URL url = new URL("http://www.anddev.org/images/tut/basic/parsingxml/example.xml");
                //dostane SAXparser ze SUXFactory
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();

                //Načte XML ze SAX parseru
                XMLReader xr = sp.getXMLReader();

                //vytvoříme obsluhu handle k XML
                XMLHandler xmlHandle = new XMLHandler();
                xr.setContentHandler(xmlHandle);

                //postará se o parsování dat z XML
                xr.parse(new InputSource(url.openStream()));

                //samotná XMLdata
                XMLdata = xmlHandle.getParsedData();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                //parser catch
                e.printStackTrace();
            } catch (SAXException e) {
                // SAX exception
                e.printStackTrace();
            } catch (IOException e) {
                // IOException z openStreamu
                e.printStackTrace();
            }
            return XMLdata;

        }

        protected void onPostExecute(ParserXMLObject xmldata){
            TextView teplota = (TextView) findViewById(R.id.CurTemp);
            teplota.setText(xmldata.toString());
        }

    }

public void kliknuto(View v){
    new Stahni().execute(); 
}


}

这里是parserXML对象

    package cz.jiri.podlipny.weather_design;

public class ParserXMLObject {
    /*
     * z XML dat vytváří objekt se kterým se dále pracuje
     */
    private String extractedString = null;
    private int extractedInt = 0;

    public String getExtractedString() {
            return extractedString;
    }
    public void setExtractedString(String extractedString) {
            this.extractedString = extractedString;
    }

    public int getExtractedInt() {
            return extractedInt;
    }
    public void setExtractedInt(int extractedInt) {
            this.extractedInt = extractedInt;
    }

    public String toString(){
            return "ExtractedString = " + this.extractedString
                            + "nExtractedInt = " + this.extractedInt;
    }
}

这里是解析器的句柄器

    package cz.jiri.podlipny.weather_design;

import java.util.jar.Attributes;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler extends DefaultHandler {
    // ===========================================================
    // Fields
    // ===========================================================

    private boolean in_outertag = false;
    private boolean in_innertag = false;
    private boolean in_mytag = false;

    private ParserXMLObject myParsedExampleDataSet = new ParserXMLObject();

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    public ParserXMLObject getParsedData() {
            return this.myParsedExampleDataSet;
    }

    // ===========================================================
    // Methods
    // ===========================================================
    @Override
    public void startDocument() throws SAXException {
            this.myParsedExampleDataSet = new ParserXMLObject();
    }

    @Override
    public void endDocument() throws SAXException {
            // Nothing to do
    }

    /** Gets be called on opening tags like:
     * <tag>
     * Can provide attribute(s), when xml was like:
    public void startElement(String namespaceURI, String localName,
                    String qName, Attributes atts) throws SAXException {
            if (localName.equals("outertag")) {
                    this.in_outertag = true;
            }else if (localName.equals("innertag")) {
                    this.in_innertag = true;
            }else if (localName.equals("mytag")) {
                    this.in_mytag = true;
            }else if (localName.equals("tagwithnumber")) {
                    // Extract an Attribute
                    String attrValue = atts.getValue("thenumber");
                    int i = Integer.parseInt(attrValue);
                    myParsedExampleDataSet.setExtractedInt(i);
            }
    }

    /** Gets be called on closing tags like:
     * </tag> */
    @Override
    public void endElement(String namespaceURI, String localName, String qName)
                    throws SAXException {
            if (localName.equals("outertag")) {
                    this.in_outertag = false;
            }else if (localName.equals("innertag")) {
                    this.in_innertag = false;
            }else if (localName.equals("mytag")) {
                    this.in_mytag = false;
            }else if (localName.equals("tagwithnumber")) {
                    // Nothing to do here
            }
    }

    /** Gets be called on the following structure:
     * <tag>characters</tag> */
    @Override
public void characters(char ch[], int start, int length) {
            if(this.in_mytag){
            myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
    }
}
}

这是LOGCAT:

04-11 12:40:48.773: I/Adreno200-EGLSUB(25887): <ConfigWindowMatch:2078>: Format RGBA_8888.
04-11 12:40:48.783: D/memalloc(25887): /dev/pmem: Mapped buffer base:0x4b966000 size:1536000 offset:0 fd:53
04-11 12:40:48.893: D/memalloc(25887): /dev/pmem: Mapped buffer base:0x4bde7000 size:9154560 offset:7618560 fd:56
04-11 12:40:51.553: D/memalloc(25887): /dev/pmem: Mapped buffer base:0x4c81c000 size:7557120 offset:6021120 fd:59
04-11 12:40:51.593: W/dalvikvm(25887): threadid=1: thread exiting with uncaught exception (group=0x40c5d1f8)
04-11 12:40:51.603: E/AndroidRuntime(25887): FATAL EXCEPTION: main
04-11 12:40:51.603: E/AndroidRuntime(25887): java.lang.IllegalStateException: Could not find a method Kliknuto(View) in the activity class cz.jiri.podlipny.weather_design.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
04-11 12:40:51.603: E/AndroidRuntime(25887):    at android.view.View$1.onClick(View.java:3039)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at android.view.View.performClick(View.java:3519)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at android.view.View$PerformClick.run(View.java:14140)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at android.os.Handler.handleCallback(Handler.java:605)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at android.os.Looper.loop(Looper.java:137)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at java.lang.reflect.Method.invoke(Method.java:511)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at dalvik.system.NativeStart.main(Native Method)
04-11 12:40:51.603: E/AndroidRuntime(25887): Caused by: java.lang.NoSuchMethodException: Kliknuto [class android.view.View]
04-11 12:40:51.603: E/AndroidRuntime(25887):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at java.lang.Class.getMethod(Class.java:915)
04-11 12:40:51.603: E/AndroidRuntime(25887):    at android.view.View$1.onClick(View.java:3032)
04-11 12:40:51.603: E/AndroidRuntime(25887):    ... 11 more
04-11 12:42:34.123: I/Adreno200-EGLSUB(25988): <ConfigWindowMatch:2078>: Format RGBA_8888.
04-11 12:42:34.163: D/memalloc(25988): /dev/pmem: Mapped buffer base:0x4bac1000 size:9154560 offset:7618560 fd:53
04-11 12:42:34.283: D/memalloc(25988): /dev/pmem: Mapped buffer base:0x4c4bc000 size:1536000 offset:0 fd:56
04-11 12:42:35.333: D/memalloc(25988): /dev/pmem: Mapped buffer base:0x4c79e000 size:7557120 offset:6021120 fd:59
04-11 12:42:35.353: W/dalvikvm(25988): threadid=1: thread exiting with uncaught exception (group=0x40c5d1f8)
04-11 12:42:35.363: E/AndroidRuntime(25988): FATAL EXCEPTION: main
04-11 12:42:35.363: E/AndroidRuntime(25988): java.lang.IllegalStateException: Could not find a method Kliknuto(View) in the activity class cz.jiri.podlipny.weather_design.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
04-11 12:42:35.363: E/AndroidRuntime(25988):    at android.view.View$1.onClick(View.java:3039)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at android.view.View.performClick(View.java:3519)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at android.view.View$PerformClick.run(View.java:14140)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at android.os.Handler.handleCallback(Handler.java:605)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at android.os.Looper.loop(Looper.java:137)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at java.lang.reflect.Method.invoke(Method.java:511)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at dalvik.system.NativeStart.main(Native Method)
04-11 12:42:35.363: E/AndroidRuntime(25988): Caused by: java.lang.NoSuchMethodException: Kliknuto [class android.view.View]
04-11 12:42:35.363: E/AndroidRuntime(25988):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at java.lang.Class.getMethod(Class.java:915)
04-11 12:42:35.363: E/AndroidRuntime(25988):    at android.view.View$1.onClick(View.java:3032)
04-11 12:42:35.363: E/AndroidRuntime(25988):    ... 11 more
04-11 13:14:30.589: I/Adreno200-EGLSUB(26277): <ConfigWindowMatch:2078>: Format RGBA_8888.
04-11 13:14:30.609: D/memalloc(26277): /dev/pmem: Mapped buffer base:0x4ba23000 size:1536000 offset:0 fd:53
04-11 13:14:30.719: D/memalloc(26277): /dev/pmem: Mapped buffer base:0x4be17000 size:7557120 offset:6021120 fd:56
04-11 13:14:32.039: D/memalloc(26277): /dev/pmem: Mapped buffer base:0x4c680000 size:9154560 offset:7618560 fd:59
04-11 13:14:32.069: W/dalvikvm(26277): threadid=1: thread exiting with uncaught exception (group=0x40c5d1f8)
04-11 13:14:32.079: E/AndroidRuntime(26277): FATAL EXCEPTION: main
04-11 13:14:32.079: E/AndroidRuntime(26277): java.lang.IllegalStateException: Could not find a method Kliknuto(View) in the activity class cz.jiri.podlipny.weather_design.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
04-11 13:14:32.079: E/AndroidRuntime(26277):    at android.view.View$1.onClick(View.java:3039)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at android.view.View.performClick(View.java:3519)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at android.view.View$PerformClick.run(View.java:14140)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at android.os.Handler.handleCallback(Handler.java:605)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at android.os.Looper.loop(Looper.java:137)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at java.lang.reflect.Method.invoke(Method.java:511)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at dalvik.system.NativeStart.main(Native Method)
04-11 13:14:32.079: E/AndroidRuntime(26277): Caused by: java.lang.NoSuchMethodException: Kliknuto [class android.view.View]
04-11 13:14:32.079: E/AndroidRuntime(26277):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at java.lang.Class.getMethod(Class.java:915)
04-11 13:14:32.079: E/AndroidRuntime(26277):    at android.view.View$1.onClick(View.java:3032)
04-11 13:14:32.079: E/AndroidRuntime(26277):    ... 11 more

1 个答案:

答案 0 :(得分:0)

在布局xml文件中,您有一个ID为button1的按钮。 onClick属性引用了一个名为Kliknuto()的方法,但您已实现了一个名为kliknuto()的方法(注意区别)。

尝试将Kliknuto替换为kliknuto属性中的onClick