0

hello guys i develop a simple app that read an XML file from the internet by using the SAX but i get this error on logcat any help could be helpful

<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>

Logcat

      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): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 10-06 08:29:52.254: E/AndroidRuntime(614): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 10-06 08:29:52.254: E/AndroidRuntime(614): ... 11 more 10-06 08:29:54.514: I/Process(614): Sending signal. PID: 614 SIG: 9

and this is the Datahandler class

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


 }

i did not know what is the error

please help

jiji
  • 11
  • 4
  • Duplicate of this http://stackoverflow.com/questions/12757873/parsing-xml-data-form-the-internet ??? – sakthisundar Oct 06 '12 at 08:21
  • yes i that question was mine too but i display this question again after i add the activity to the manifest file but still get the same error – jiji Oct 06 '12 at 08:23

2 Answers2

0

Where do you call DataHandler.getData? If this is in an activity constructor or on create call this will lead to a NetworkOnMainThreadException.

If this is the case please look into AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html

You would need to call getData in doInBackground.

Ian Newson
  • 7,679
  • 2
  • 47
  • 80
  • yest it is in the oncreate method in the main method so how can i solve this error – jiji Oct 06 '12 at 08:25
  • You can't do network operations such and URL.openConnection on the main thread, so you need to use AsyncTask. – Ian Newson Oct 06 '12 at 08:27
-1

You don't have a nullary constructor Jiji. You should have an empty constructor in order to use newInstance method like I have mentioned in the previous question itself. Something like

public DataHandler() {
}
sakthisundar
  • 3,278
  • 3
  • 16
  • 29
  • ok i did what you say and the error about the constructor was disappear thank you but what about the hall other errors ? – jiji Oct 06 '12 at 08:44