Newbie... Made a working XML reader, but classes dont work

Put your problem here if it does not fit any of the other categories.

Newbie... Made a working XML reader, but classes dont work

Postby bennyq » Sun Feb 05, 2012 5:54 am

Okay so my first app, I got an XML parser working. In the same class it works fine, but if i seperate it to a new class/java file, It doesnt work, compiles fine but wont load in AVD. Could someone please look at my classes, ill include im imports etc as well because i think it has something to do with the namespace...Any help much appreciated

My Main entry point, supposed to load home.xml
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.helloworld;
  2.  
  3. import android.app.Activity;
  4. import java.io.IOException;
  5. import org.xmlpull.v1.XmlPullParser;
  6. import org.xmlpull.v1.XmlPullParserException;
  7. //import android.app.Activity;
  8. import android.content.res.Resources;
  9. import android.content.res.XmlResourceParser;
  10. import android.os.Bundle;
  11. import android.widget.TextView;
  12. // import com.helloworld.XMLParse;
  13.  
  14. //import android.widget.TextView;
  15.  
  16. public class HelloworldActivity extends Activity {
  17.     /** Called when the activity is first created. */
  18.     @Override
  19.         public void onCreate(Bundle savedInstanceState) {
  20.             super.onCreate(savedInstanceState);
  21.        
  22.             setContentView(R.layout.home);
  23.             XMLParse xml_parse = new XMLParse();
  24.             xml_parse.doXML();
  25.            
  26.  
  27.            
  28.         }
  29.    
  30. }
  31.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



This file XMLParse.java is supposed to the job for me...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.helloworld;
  2. import java.io.IOException;
  3.  
  4. import org.xmlpull.v1.XmlPullParser;
  5. import org.xmlpull.v1.XmlPullParserException;
  6.  
  7. import android.app.Activity;
  8. import android.content.res.Resources;
  9. import android.content.res.XmlResourceParser;
  10. import android.widget.TextView;
  11.  
  12. public class XMLParse extends Activity {
  13.        
  14.     public void doXML() {
  15.        
  16.         TextView myXmlContent = (TextView)findViewById(R.id.my_xml);
  17.         String stringXmlContent;
  18.                 try {
  19.                         stringXmlContent = getEventsFromAnXML(this);
  20.                         myXmlContent.setText(stringXmlContent);
  21.                 } catch (XmlPullParserException e) {
  22.                         // TODO Auto-generated catch block
  23.                         e.printStackTrace();
  24.                 } catch (IOException e) {
  25.                         // TODO Auto-generated catch block
  26.                         e.printStackTrace();
  27.                 }
  28.        
  29.     }
  30.    
  31.     private String getEventsFromAnXML(Activity activity)
  32.             throws XmlPullParserException, IOException
  33.             {
  34.                 StringBuffer stringBuffer = new StringBuffer();
  35.                 Resources res = activity.getResources();
  36.                 XmlResourceParser xpp = res.getXml(R.xml.myxml);
  37.                 xpp.next();
  38.                 int eventType = xpp.getEventType();
  39.                 while (eventType != XmlPullParser.END_DOCUMENT)
  40.                 {
  41.                         if(eventType == XmlPullParser.START_DOCUMENT)
  42.                         {
  43.                                 stringBuffer.append("--- Start XML ---");
  44.                         }
  45.                         else if(eventType == XmlPullParser.START_TAG)
  46.                         {
  47.                                 stringBuffer.append("\nSTART_TAG: "+xpp.getName());
  48.                         }
  49.                         else if(eventType == XmlPullParser.END_TAG)
  50.                         {
  51.                                 stringBuffer.append("\nEND_TAG: "+xpp.getName());
  52.                         }
  53.                         else if(eventType == XmlPullParser.TEXT)
  54.                         {
  55.                                 stringBuffer.append("\nTEXT: "+xpp.getText());
  56.                         }
  57.                         eventType = xpp.next();
  58.                 }
  59.                 stringBuffer.append("\n--- End XML ---");
  60.                 return stringBuffer.toString();
  61.             }
  62. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
bennyq
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 05, 2012 5:45 am

Top

Re: Newbie... Made a working XML reader, but classes dont wo

Postby Phyll » Sun Feb 05, 2012 7:11 am

Hi bennyq,

Your XMLParse class should not extend Activity.

You need to make a constructor for it and send the Context of your activity to it to store and pass on to methods that need it. Like this:

Code: Select all

public class XMLParse {

    private Context context = null;

    public XMLParse( Context ctx) {
        context = ctx;
        }; //constructor

...Rest of the class



You should not try to access a TextView in your Activity from there. You should return the string(s) and then set them in the activity.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Newbie... Made a working XML reader, but classes dont wo

Postby bennyq » Sun Feb 05, 2012 7:12 am

Resolved...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.helloworld;
  2.     import java.io.IOException;
  3.  
  4. import org.xmlpull.v1.XmlPullParser;
  5. import org.xmlpull.v1.XmlPullParserException;
  6.  
  7. import android.content.Context;
  8. import android.content.res.XmlResourceParser;
  9.      
  10.     public class XMLParse {
  11.            
  12.         public String doXML(Context context) {
  13.  
  14.                     try {
  15.                             return getEventsFromAnXML(context);
  16.                            
  17.                     } catch (XmlPullParserException e) {
  18.                             // TODO Auto-generated catch block
  19.                        
  20.                             e.printStackTrace();
  21.                             return "";
  22.                     } catch (IOException e) {
  23.                        
  24.                             // TODO Auto-generated catch block
  25.                             e.printStackTrace();
  26.                             return"";
  27.                     }
  28.            
  29.         }
  30.        
  31.         private String getEventsFromAnXML(Context context)
  32.                 throws XmlPullParserException, IOException
  33.                 {
  34.                     StringBuffer stringBuffer = new StringBuffer();
  35.                     XmlResourceParser xpp = context.getResources().getXml(R.xml.myxml);
  36.                     xpp.next();
  37.                     int eventType = xpp.getEventType();
  38.                     while (eventType != XmlPullParser.END_DOCUMENT)
  39.                     {
  40.                             if(eventType == XmlPullParser.START_DOCUMENT)
  41.                             {
  42.                                     stringBuffer.append("--- Start XML ---");
  43.                             }
  44.                             else if(eventType == XmlPullParser.START_TAG)
  45.                             {
  46.                                     stringBuffer.append("\nSTART_TAG: "+xpp.getName());
  47.                             }
  48.                             else if(eventType == XmlPullParser.END_TAG)
  49.                             {
  50.                                     stringBuffer.append("\nEND_TAG: "+xpp.getName());
  51.                             }
  52.                             else if(eventType == XmlPullParser.TEXT)
  53.                             {
  54.                                     stringBuffer.append("\nTEXT: "+xpp.getText());
  55.                             }
  56.                             eventType = xpp.next();
  57.                     }
  58.                     stringBuffer.append("\n--- End XML ---");
  59.                     return stringBuffer.toString();
  60.                 }
  61.     }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4




and main file loads with
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             XMLParse xml_parse = new XMLParse();
  2.            
  3.             TextView myXmlContent = (TextView)findViewById(R.id.my_xml);
  4.  
  5.             myXmlContent.setText ( xml_parse.doXML(this));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Thanks 2 QubeZ #android-dev
bennyq
Freshman
Freshman
 
Posts: 2
Joined: Sun Feb 05, 2012 5:45 am

Re: Newbie... Made a working XML reader, but classes dont wo

Postby Phyll » Sun Feb 05, 2012 7:14 am

Guess you're too fast for me.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 21 guests