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

Apps Activity won't load data when launched

Discussion in 'Android Development' started by cr5315, Feb 21, 2011.

  1. cr5315

    cr5315 Android Enthusiast
    Thread Starter

    I have an activity that loads an RSS feed in its onCreate and it works fine, when the app is the default activity. I've taken the code and integrated it into an application that uses tabs. The activity loads its layout, but not the info from the RSS. I'm almost completely sure that the problem has something to do with the activity not being the default activity. I've built other apps that use the same RSS code and they work just fine.

    AndroidRssReader.java
    Code (Text):
    1. package com.bba.supercross;
    2.  
    3. import java.io.IOException;
    4. import java.net.MalformedURLException;
    5. import java.net.URL;
    6.  
    7.  
    8. import javax.xml.parsers.ParserConfigurationException;
    9. import javax.xml.parsers.SAXParser;
    10. import javax.xml.parsers.SAXParserFactory;
    11.  
    12. import org.xml.sax.InputSource;
    13. import org.xml.sax.SAXException;
    14. import org.xml.sax.XMLReader;
    15.  
    16. import android.app.ListActivity;
    17. import android.content.Intent;
    18. import android.os.Bundle;
    19. import android.view.View;
    20. import android.widget.ArrayAdapter;
    21. import android.widget.ListView;
    22.  
    23.  
    24. public class AndroidRssReader extends ListActivity {
    25.  
    26. private RSSFeed myRssFeed = null;
    27.  
    28.   /** Called when the activity is first created. */
    29.   @Override
    30.   public void onCreate(Bundle savedInstanceState) {
    31.       super.onCreate(savedInstanceState);
    32.       setContentView(R.layout.news);
    33.      
    34.      
    35.    
    36.       try {
    37.   URL rssUrl = new URL("http://www.supercross.com/news/feed/rss");
    38.   SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
    39.   SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
    40.   XMLReader myXMLReader = mySAXParser.getXMLReader();
    41.   RSSHandler myRSSHandler = new RSSHandler();
    42.   myXMLReader.setContentHandler(myRSSHandler);
    43.   InputSource myInputSource = new InputSource(rssUrl.openStream());
    44.   myXMLReader.parse(myInputSource);
    45.  
    46.   myRssFeed = myRSSHandler.getFeed();
    47.  
    48.  } catch (MalformedURLException e) {
    49.   // TODO Auto-generated catch block
    50.   e.printStackTrace();
    51.  } catch (ParserConfigurationException e) {
    52.   // TODO Auto-generated catch block
    53.   e.printStackTrace();
    54.  } catch (SAXException e) {
    55.   // TODO Auto-generated catch block
    56.   e.printStackTrace();
    57.  } catch (IOException e) {
    58.   // TODO Auto-generated catch block
    59.   e.printStackTrace();
    60.  }
    61.  
    62.  if (myRssFeed!=null)
    63.  {
    64.  
    65.  
    66.   ArrayAdapter<RSSItem> adapter =
    67.    new ArrayAdapter<RSSItem>(this,
    68.      android.R.layout.simple_list_item_1,myRssFeed.getList());
    69.   setListAdapter(adapter);
    70.  
    71.  }
    72.   }
    73.  
    74.  
    75.        
    76. @Override
    77. protected void onListItemClick(ListView l, View v, int position, long id) {
    78.  // TODO Auto-generated method stub
    79.  Intent intent = new Intent(this,ShowDetails.class);
    80.  Bundle bundle = new Bundle();
    81.  bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
    82.  bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
    83.  bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
    84.  bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
    85.  intent.putExtras(bundle);
    86.       startActivity(intent);
    87. }
    88.  
    89. }
    90.  
    TabBar.java
    Code (Text):
    1.  
    2. package com.bba.supercross;
    3.  
    4. import android.app.TabActivity;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.view.Menu;
    8. import android.view.MenuInflater;
    9. import android.view.MenuItem;
    10. import android.widget.TabHost;
    11. import android.widget.TabHost.TabSpec;
    12.  
    13. public class TabBar extends TabActivity {
    14.     /** Called when the activity is first created. */
    15.     @Override
    16.     public void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         setContentView(R.layout.tab);
    19.  
    20.         /** TabHost will have Tabs */
    21.         TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
    22.  
    23.         /** TabSpec used to create a new tab.
    24.          * By using TabSpec only we can able to setContent to the tab.
    25.          * By using TabSpec setIndicator() we can set name to tab. */
    26.  
    27.         /** tid1 is firstTabSpec Id. Its used to access outside. */
    28.         TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
    29.         TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
    30.  
    31.         /** TabSpec setIndicator() is used to set name for the tab. */
    32.         /** TabSpec setContent() is used to set content for a particular tab. */
    33.         firstTabSpec.setIndicator("Race Results").setContent(new Intent(this,FirstTab.class));
    34.         secondTabSpec.setIndicator("News").setContent(new Intent(this,SecondTab.class));
    35.  
    36.         /** Add tabSpec to the TabHost to display. */
    37.         tabHost.addTab(firstTabSpec);
    38.         tabHost.addTab(secondTabSpec);
    39.  
    40.     }
    41.     @Override
    42.     public boolean onCreateOptionsMenu(Menu menu) {
    43.         MenuInflater inflater = getMenuInflater();
    44.         inflater.inflate(R.menu.menu, menu);
    45.         return true;
    46.     }
    47.     @Override
    48.     public boolean onOptionsItemSelected(MenuItem item) {
    49.         // Handle item selection
    50.         switch (item.getItemId()) {
    51.         case R.id.Menu1:
    52.         Intent about = new Intent (this, SuperAbout.class);
    53.         startActivity(about);
    54.         default:
    55.             return super.onOptionsItemSelected(item);
    56.         }
    57.     }
    58. }
    59.  
     

    Advertisement

  2. pietjuhhh1990

    pietjuhhh1990 Well-Known Member

    You need to start the intent

    Code (Text):
    1.  
    2. secondTabSpec.setIndicator("News").setContent(new Intent i(this,SecondTab.class));
    3. startActivity(i);
    4.  
     
  3. cr5315

    cr5315 Android Enthusiast
    Thread Starter

    When I put that in the code, it underlined the i's and told me to delete those tokens. Am I putting it in the right spot?

    Code (Text):
    1.  /** TabSpec setIndicator() is used to set name for the tab. */
    2.         /** TabSpec setContent() is used to set content for a particular tab. */
    3.         firstTabSpec.setIndicator("Race Results").setContent(new Intent(this,FirstTab.class));
    4.         secondTabSpec.setIndicator("News").setContent(new Intent i(this,AndroidRssReader.class));
    5.         startActivity(i);
     
  4. pietjuhhh1990

    pietjuhhh1990 Well-Known Member

    You can also set it in the manifest how it should react because now the scritp is only using the layout of the RSS reader.

    use this link:
    Intents and Intent Filters | Android Developers

    Here you can set how the intent should make the class react
     
Loading...
Similar Threads - Activity won't load
  1. Android News
    Replies:
    0
    Views:
    61
  2. umtblbl
    Replies:
    0
    Views:
    270
  3. riveraadrian
    Replies:
    13
    Views:
    150
  4. kowal
    Replies:
    6
    Views:
    547
  5. muhammadzohaibsattar
    Replies:
    1
    Views:
    391
  6. Piranha97
    Replies:
    1
    Views:
    108
  7. Guillaume C
    Replies:
    1
    Views:
    196
  8. David Pesetsky
    Replies:
    11
    Views:
    508
  9. Alvaro Vargas
    Replies:
    1
    Views:
    614
  10. vellangelus
    Replies:
    1
    Views:
    184

Share This Page

Loading...