繁体   English   中英

xml解析器和Android中的线程

[英]xml parser and thread in Android

我有两个班:MainActivity和Quake。

package com.example.ex24;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

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

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

import android.app.ListFragment;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;

public class MainActivity extends ListFragment {
ArrayAdapter<Quake> aa ;
ArrayList<Quake> earthquake  = new ArrayList<Quake>();
private Handler handler = new Handler();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    int layoutid = android.R.layout.simple_expandable_list_item_1 ;
    aa = new ArrayAdapter<Quake>(getActivity(), layoutid, earthquake);
    setListAdapter(aa);

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            refreshQ();
        }
    });t.start();
}

public void refreshQ(){
    URL url ;

    try{
    String quakefeed = getString(R.string.quake_feed);
    url = new URL(quakefeed);
    URLConnection connection ;
    connection = url.openConnection();
    HttpURLConnection httpconnection = (HttpURLConnection)connection;
    int responsecode = httpconnection.getResponseCode();

    if(responsecode == HttpURLConnection.HTTP_OK){

        InputStream in = httpconnection.getInputStream();
        DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder() ;

        Document dom = db.parse(in);
        Element docelm = dom.getDocumentElement();
        earthquake.clear();
        NodeList nl = docelm.getElementsByTagName("entry");
        if(nl != null && nl.getLength() > 0 ){

            for(int i = 0 ; i < nl.getLength() ; ++i ){

                Element entry = (Element)nl.item(i);
                Element title =    (Element)entry.getElementsByTagName("title").item(0);

                String title_str =    title.getFirstChild().getNodeValue();
                final Quake quake = new Quake(title_str);
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        addNewQ(quake);
                    }
                });
            }

        }




    }


    } catch (SAXException e) {
        // TODO: handle exception
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private void addNewQ(Quake _quake){
    earthquake.add(_quake);
    aa.notifyDataSetChanged();
}

}

package com.example.ex24;

public class Quake {

String detalis ;

public String getDetalis(){return detalis ;}

public Quake( String _str ) {
    // TODO Auto-generated constructor stub
    detalis = _str ;
}
@Override
public String toString() {
    // TODO Auto-generated method stub
    return "Detalis :" + detalis ;
}

}

XML格式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<fragment 
    android:name="com.example.ex24.MainActivity"
    android:id="@+id/EarthquakeListFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">ex24</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="quake_feed">http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml</string>


现在,为什么会出现此错误?

不幸的是,ex24已停止。

12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:03.011: D/AndroidRuntime(1187): Shutting down VM
12-24 16:49:03.011: W/dalvikvm(1187): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-24 16:49:03.091: E/AndroidRuntime(1187): FATAL EXCEPTION: main
12-24 16:49:03.091: E/AndroidRuntime(1187): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ex24/com.example.ex24.MainActivity}: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at android.os.Looper.loop(Looper.java:137)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at java.lang.reflect.Method.invokeNative(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at java.lang.reflect.Method.invoke(Method.java:511)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at dalvik.system.NativeStart.main(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187): Caused by: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-24 16:49:03.091: E/AndroidRuntime(1187):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-24 16:49:03.091: E/AndroidRuntime(1187):     ... 11 more

这是真的?

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ex24"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.ex24.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

我想您已在清单中将“活动”声明为一项活动。 但是,这是一个片段。 系统尝试启动它,并将其转换为流程中的Activity,但这显然是不可能的,因为它是一个片段。

您应该将“类MainActivity extended ListFragment”更改为

class MainActivity extends ListActivity

目前,您的MainActivity不是活动,而是片段。

但是在您的情况下,您真正​​想要的是一个活动

您还会遇到一些其他错误,因为Activity具有与Fragment不同的方法

笔记:

片段不需要在清单中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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