0

大家好,我开发了一个简单的应用程序,该应用程序使用 SAX 从 Internet 读取 XML 文件,但我在 logcat 上收到此错误任何帮助可能会有所帮助

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reaf.xml"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
    android:name="com.read.xml.DataHandler"
    android:label="@string/title_activity_main" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

日志猫

      10-06 08:29:52.204: D/AndroidRuntime(614): Shutting down VM
      10-06 08:29:52.204: W/dalvikvm(614): threadid=1: thread exiting with uncaught  exception (group=0x40015560)
      10-06 08:29:52.254: E/AndroidRuntime(614): FATAL EXCEPTION: main
      10-06 08:29:52.254: E/AndroidRuntime(614): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.read.xml/com.read.xml.DataHandler}:  java.lang.ClassCastException: com.read.xml.DataHandler
     10-06 08:29:52.254: E/AndroidRuntime(614):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
      10-06 08:29:52.254: E/AndroidRuntime(614):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
       10-06 08:29:52.254: E/AndroidRuntime(614):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
     10-06 08:29:52.254: E/AndroidRuntime(614):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
       10-06 08:29:52.254: E/AndroidRuntime(614):   at android.os.Handler.dispatchMessage(Handler.java:99)
      10-06 08:29:52.254: E/AndroidRuntime(614):    at android.os.Looper.loop(Looper.java:123)
     10-06 08:29:52.254: E/AndroidRuntime(614):     at android.app.ActivityThread.main(ActivityThread.java:3683)
    10-06 08:29:52.254: E/AndroidRuntime(614):  at java.lang.reflect.Method.invokeNative(Native Method)
    10-06 08:29:52.254: E/AndroidRuntime(614):  at java.lang.reflect.Method.invoke(Method.java:507)
   10-06 08:29:52.254: E/AndroidRuntime(614):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    10-06 08:29:52.254: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

10-06 08:29:52.254: E/AndroidRuntime(614): at dalvik.system.NativeStart.main(Native Method) 10-06 08:29:52.254: E/AndroidRuntime(614): Caused by: java.lang .ClassCastException: com.read.xml.DataHandler 10-06 08:29:52.254: E/AndroidRuntime(614): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 10-06 08:29:52.254: E/AndroidRuntime(614): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 10-06 08:29:52.254: E/AndroidRuntime(614): ... 11 更多 10-06 08:29: 54.514:I/Process(614):发送信号。PID:614 SIG:9

这是 Datahandler 类

               package com.read.xml;

  import java.io.BufferedReader;
  import java.io.IOException;
   import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
     import java.util.ArrayList;

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

   import org.xml.sax.Attributes;
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
  import org.xml.sax.XMLReader;
  import org.xml.sax.helpers.DefaultHandler;

  import android.content.Context;
  import android.graphics.Color;
 import android.util.Log;
 import android.widget.TextView;

   public class DataHandler extends DefaultHandler {

//list for imported product data
private ArrayList<TextView> theViews;
//string to track each entry
private String currBrand = "";
//flag to keep track of XML processing
private boolean isProduct = false;
//context for user interface
private Context theContext;
//constructor
public DataHandler(Context cont) {
    super();
    theViews = new ArrayList<TextView>();
    theContext = cont;
 }
//start of the XML document
public void startDocument ()
{ Log.i("DataHandler", "Start of XML document"); }

//end of the XML document
public void endDocument ()
{ Log.i("DataHandler", "End of XML document"); }

//opening element tag
public void startElement (String uri, String name, String qName, Attributes atts)
{
    //find out if the element is a brand
    if(qName.equals("brand"))
    {
        //set product tag to false
        isProduct = false;
        //create View item for brand display
        TextView brandView = new TextView(theContext);
        brandView.setTextColor(Color.rgb(73, 136, 83));
        //add the attribute value to the displayed text
        String viewText = "Items from " + atts.getValue("name") + ":";
        brandView.setText(viewText);
        //add the new view to the list
        theViews.add(brandView);
    }
    //the element is a product
    else if(qName.equals("product"))
        isProduct = true;
}

//closing element tag
public void endElement (String uri, String name, String qName)
{
    if(qName.equals("brand"))
    {
        //create a View item for the products
        TextView productView = new TextView(theContext);
        productView.setTextColor(Color.rgb(192, 199, 95));
        //display the compiled items
        productView.setText(currBrand);
        //add to the list
        theViews.add(productView);
        //reset the variable for future items
        currBrand = "";
    }
}

//element content
public void characters (char ch[], int start, int length)
{
    //string to store the character content
    String currText = "";
    //loop through the character array
    for (int i=start; i<start+length; i++)
    {
        switch (ch[i]) {
        case '\\':
            break;
        case '"':
            break;
        case '\n':
            break;
        case '\r':
            break;
        case '\t':
            break;
        default:
            currText += ch[i];
            break;
        }
    }
    //prepare for the next item
    if(isProduct && currText.length()>0)
        currBrand += currText+"\n";
}

public ArrayList<TextView> getData()
{
    //take care of SAX, input and parsing errors
    try
    {
            //set the parsing driver
        System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
            //create a parser
        SAXParserFactory parseFactory = SAXParserFactory.newInstance();
        SAXParser xmlParser = parseFactory.newSAXParser();
            //get an XML reader
        XMLReader xmlIn = xmlParser.getXMLReader();
            //instruct the app to use this object as the handler
        xmlIn.setContentHandler(this);
            //provide the name and location of the XML file
        URL xmlURL = new URL("www.data-online.bugs3.com/public_html/test.xml");
            //open the connection and get an input stream
        URLConnection xmlConn = xmlURL.openConnection();
        InputStreamReader xmlStream = new    
           InputStreamReader(xmlConn.getInputStream());
            //build a buffered reader
        BufferedReader xmlBuff = new BufferedReader(xmlStream);
            //parse the data
        xmlIn.parse(new InputSource(xmlBuff));
    }
    catch(SAXException se) { Log.e("AndroidTestsActivity", 
            "SAX Error " + se.getMessage()); }
    catch(IOException ie) { Log.e("AndroidTestsActivity", 
            "Input Error " + ie.getMessage()); }
    catch(Exception oe) { Log.e("AndroidTestsActivity", 
            "Unspecified Error " + oe.getMessage()); }
        //return the parsed product list
    return theViews;
}


 }

我不知道错误是什么

请帮忙

4

2 回答 2

0

你在哪里调用 DataHandler.getData?如果这是在活动构造函数中或在创建调用中,这将导致 NetworkOnMainThreadException。

如果是这种情况,请查看 AsyncTask:http: //developer.android.com/reference/android/os/AsyncTask.html

您需要在 doInBackground 中调用 getData。

于 2012-10-06T08:23:19.340 回答
-1

你没有一个 nullary 构造函数 Jiji。您应该有一个空的构造函数,以便使用我在上一个问题本身中提到的 newInstance 方法。就像是

public DataHandler() {
}
于 2012-10-06T08:26:41.400 回答