1. Are you ready for the Galaxy S20? Here is everything we know so far!

XmlPullParser problem

Discussion in 'Android Development' started by novak27, Oct 15, 2014.

  1. novak27

    novak27 Lurker
    Thread Starter

    hey all. I am just beginner for android. just now i am developing an application by using TabHost. In the app i develop i want to parse a XML source from url in the second tab. But when i parse it then the array is always empty. Here is my code.
    [HIGH]
    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_second);


    arrCategoryLink = new ArrayList<String>();
    arrCategoryTitle = new ArrayList<String>();

    lstCategory = (ListView) findViewById(R.id.lstCategories);
    lstCategory.setOnItemClickListener(this);



    try {

    XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
    pullParserFactory.setNamespaceAware(true);
    XmlPullParser parser = pullParserFactory.newPullParser();
    URL url = new URL("sourceURL");
    InputStream stream = url.openStream();

    parser.setInput(stream, null);

    int eventType = parser.getEventType();

    while (eventType != XmlPullParser.END_DOCUMENT) {

    if (eventType == XmlPullParser.START_DOCUMENT) {

    } else if (eventType == XmlPullParser.START_TAG) {

    if (parser.getName().equalsIgnoreCase("link")) {
    // Log.i("LINK", parser.nextText().toString());
    arrCategoryTitle.add(parser.nextText().toString());
    }
    else if (parser.getName().equalsIgnoreCase("title")) {
    // Log.i("title", parser.nextText().toString());
    arrCategoryLink.add(parser.nextText().toString());
    }
    }

    eventType = parser.next();
    }

    } catch (Exception e) {
    e.printStackTrace();
    }

    ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrCategoryTitle);
    lstCategory.setAdapter(myAdapter);

    Log.i("SecondActivity", "Count " + arrCategoryLink.size());

    }
    [/HIGH]
    the weird when i write this in another project then it parses well but when i use TabHost then it doesnt work. what can be the problem ?
     



    1. Download the Forums for Android™ app!


      Download

       
  2. novak27

    novak27 Lurker
    Thread Starter

    Log.i("SecondAc", ex.getMessage().toString());

    this line crashes the application.

    Second problem the arrays are empty

    What is wrong here ?
     
Loading...

Share This Page

Loading...