Question

I am currently making an application that parses an RSS Feed. I have used an XML pull parser in order to display the information from the feed into a Listview. I want to create a new activity that launches when the user clicks on any parsed title in the ListView. In the new activity, I want some more information parsed from RSS feed, that is related to the item that has just been clicked.

I've tried several things to try and get a new view to display, but unfortunately the app keeps crashing. I can display the extra information I need in a dialog box when an item in the list is clicked.

This is the code I have, and the last part at the bottom is where I have been trying to link it to a new view.

      public class RssFeed extends ListActivity {


  List titles;
  List description;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rss);

    titles = new ArrayList();
    description = new ArrayList();

    try {
        URL url = new        URL("www.sample.com");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(getInputStream(url), "UTF_8");
        boolean insideItem = false;

        /** While the rss feed has not displayed end_document, pull the title and description information */
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase("title")) {
                    if (insideItem)
                        titles.add(xpp.nextText()); 
                } else if (xpp.getName().equalsIgnoreCase("description")) {
                    if (insideItem)
                        description.add(xpp.nextText()); 
                }
            }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                insideItem=false;
            }

            eventType = xpp.next(); 
        }


    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, titles);
    setListAdapter(adapter);



        }

        public InputStream getInputStream(URL url) {
                   try {
                       return url.openConnection().getInputStream();
                   } catch (IOException e) {
                       return null;
                     }
                }

      //When the user clicks on a list this runs
     protected void onListItemClick(ListView l, View v, int position, long id) {
     Intent intent = new Intent(getActivity(), Descriptions.class);
                startActivity(intent);

The new activity simply contains code to set the view.

    import android.os.Bundle;
    import android.app.Activity;


    public class Descriptions extends Activity {


        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             setContentView(R.layout.description);
}
    }

Log Cat -

04-03 19:31:03.446: D/dalvikvm(564): GC freed 8121 objects / 330568 bytes in 85ms
04-03 19:31:09.856: D/AndroidRuntime(564): Shutting down VM
04-03 19:31:09.856: W/dalvikvm(564): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-03 19:31:09.856: E/AndroidRuntime(564): Uncaught handler: thread main exiting due to uncaught exception
04-03 19:31:09.876: E/AndroidRuntime(564): java.lang.NullPointerException
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.content.ComponentName.<init>(ComponentName.java:75)
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.content.Intent.<init>(Intent.java:2551)
04-03 19:31:09.876: E/AndroidRuntime(564):  at org.me.myandroidstuff.TrafficScotlandPrototype.RssFeed.onListItemClick(RssFeed.java:109)
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.app.ListActivity$2.onItemClick(ListActivity.java:312)
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.widget.ListView.performItemClick(ListView.java:3285)
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.os.Handler.handleCallback(Handler.java:587)
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.os.Looper.loop(Looper.java:123)
04-03 19:31:09.876: E/AndroidRuntime(564):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-03 19:31:09.876: E/AndroidRuntime(564):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 19:31:09.876: E/AndroidRuntime(564):  at java.lang.reflect.Method.invoke(Method.java:521) 
04-03 19:31:09.876: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
04-03 19:31:09.876: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
04-03 19:31:09.876: E/AndroidRuntime(564):  at dalvik.system.NativeStart.main(Native Method)
04-03 19:31:09.896: I/dalvikvm(564): threadid=7: reacting to signal 3
04-03 19:31:09.906: I/dalvikvm(564): Wrote stack trace to '/data/anr/traces.txt'
Was it helpful?

Solution

You avtually have to provide the context of the activity in the intent that you create.

so your code is going to be like this

public void registerCallClickBack() {
        ListView list = (ListView) findViewById(your list);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked,
                    int position, long id) {
                Intent intent = new Intent(MainActivity.this, Descriptions.class);
                startActivity(intent);
            }
        });
    } 

and in your MainActivity in onCreate method you need

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             setContentView(R.layout.description);
             registerCallClickBack();

OTHER TIPS

One thing I would say is to write the @Override tag for onListItemClick since you are overriding it. Also Is Descriptions.java declared in your manifest file?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top