user3183582
user3183582

Reputation:

Parsing XML, i have no errors, i don't know whats wrong?

I am trying to get rss feeds in my ListView and then open the description of each list row.i have no idea why my project closes.what am i doing wrong ?

This is my code:

MainActivity.java

public class MainActivity extends Activity {

 // A reference to the local object
private MainActivity local;

/**
 * This method creates main application view
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set view
    setContentView(R.layout.activity_main);

    // Set reference to this activity
    local = this;

    GetRSSDataTask task = new GetRSSDataTask();

    // Start download RSS task
    task.execute("http://www.conciencia.net/rss.aspx");

}

private class GetRSSDataTask extends AsyncTask<String, Void, List<RssAtomItem> > {
    @Override
    protected List<RssAtomItem> doInBackground(String... urls) {

        try {
            // Create RSS reader
            RssAtomReader rssReader = new RssAtomReader(urls[0]);

            // Parse RSS, get items
            return rssReader.getItems();

        } catch (Exception e) {
            Log.e("ITCRssAtomReader", e.getMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(List<RssAtomItem> result) {

        // Get a ListView from main view
        ListView itcItems = (ListView) findViewById(R.id.listMainView);

        // Create a list adapter
        ArrayAdapter<RssAtomItem> adapter = new ArrayAdapter<RssAtomItem>(local,android.R.layout.simple_list_item_1, result);
        // Set list adapter for the ListView
        itcItems.setAdapter(adapter);

        // Set list view item click listener
        itcItems.setOnItemClickListener(new ListListener(result, local));
    }
}

}

DetailsActivity.java

public class DetailsActivity extends Activity {

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

    String title    = (String)getIntent().getExtras().get("title");
    String author   = (String)getIntent().getExtras().get("author");
    String guid     = (String)getIntent().getExtras().get("guid");
    String description  = (String)getIntent().getExtras().get("description");


    Log.d("DEBUG", "title:\t" + title);
    Log.d("DEBUG", "author:\t" + author);
    Log.d("DEBUG", "guid:\t" + guid);
    Log.d("DEBUG", "description:\t\t" + description);


    TextView titleTV = (TextView)findViewById(R.id.detailsTextView);
    TextView titleTV2 = (TextView)findViewById(R.id.detailsTextView2);
    TextView titleTV3 = (TextView)findViewById(R.id.detailsTextView3);
    TextView titleTV4 = (TextView)findViewById(R.id.detailsTextView4);


    titleTV.setText(title);
    titleTV2.setText(title);
    titleTV3.setText(title);
    titleTV4.setText(title);


}


}

RssAtomItem.java

public class RssAtomItem {

private String title;

private String author;

private String guid;

private String description;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getGuid() {
    return guid;
}

public void setGuid(String guid) {
    this.guid = guid;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

@Override
public String toString() {
    return title;
}

}

ListListener.java

public class ListListener implements OnItemClickListener {

// List item's reference
List<RssAtomItem> listItems;
// Calling activity reference
Activity activity;

public ListListener(List<RssAtomItem> aListItems, Activity anActivity) {
    listItems = aListItems;
    activity  = anActivity;
}

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
    Intent i = new Intent(activity, DetailsActivity.class);
    i.setData(Uri.parse(listItems.get(pos).getDescription()));

    i.putExtra("title", listItems.get(pos).getTitle());
    i.putExtra("author", listItems.get(pos).getAuthor());
    i.putExtra("guid", listItems.get(pos).getGuid());
    i.putExtra("description", listItems.get(pos).getDescription());

    activity.startActivity(i);

}

}

RssAtomParseHandler.java

public class RssAtomParseHandler extends DefaultHandler {

private List<RssAtomItem> rssItems;

// Used to reference item while parsing
private RssAtomItem currentItem;

// Parsing title indicator
private boolean parsingTitle;
// Parsing link indicator
private boolean parsingContents;
// A buffer for title contents
private StringBuffer currentTitleSb;
// A buffer for content tag contents
private StringBuffer currentContentSb;

public RssAtomParseHandler() {
    rssItems = new ArrayList<RssAtomItem>();
}

public List<RssAtomItem> getItems() {
    return rssItems;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

    if ("entry".equals(qName)) {
        currentItem = new RssAtomItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;
        currentTitleSb = new StringBuffer();
    } else if ("description".equals(qName)) {
        parsingContents = true;
        currentContentSb = new StringBuffer();
    }
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

    if ("entry".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {
        parsingTitle = false;

        if (currentItem != null) // There is a title tag for a whole channel present. It is being parsed before the entry tag is present, so we need to check if item is not null
            currentItem.setTitle(currentTitleSb.toString());

    } else if ("description".equals(qName)) {
        parsingContents = false;

        if (currentItem != null)
            currentItem.setDescription(currentContentSb.toString());

    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {

    if (parsingTitle) {
        if (currentItem != null)
            currentTitleSb.append(new String(ch, start, length));
    } else if (parsingContents) {
        if (currentItem != null)
            currentContentSb.append(new String(ch, start, length));
    }
}

}

RssAtomReader.java

public class RssAtomReader {

private String rssUrl;

/**
 * Constructor
 * 
 * @param rssUrl
 */
public RssAtomReader(String rssUrl) {
    this.rssUrl = rssUrl;
}

/**
 * Get RSS items.
 * 
 * @return
 */
public List<RssAtomItem> getItems() throws Exception {
    // SAX parse RSS data
    SAXParserFactory factory = SAXParserFactory.newInstance();

    SAXParser saxParser = factory.newSAXParser();

    RssAtomParseHandler handler = new RssAtomParseHandler();

    saxParser.parse(rssUrl, handler);

    return handler.getItems();

}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backgroundm"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
    android1:id="@+id/listMainView"
    android1:layout_width="match_parent"
    android1:layout_height="590dp"
    android1:layout_alignParentTop="true"
    android1:layout_centerHorizontal="true"
    android1:layout_marginTop="125dp" >
</ListView>

</RelativeLayout>

activity_details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/detailsTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[POST TITLE GOES HERE]"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textStyle="bold" />

<TextView
    android:id="@+id/detailsTextView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[POST AUTHOR GOES HERE]"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/detailsTextView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[POST GUID GOES HERE]"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/detailsTextView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[POST DESCRIPTION GOES HERE]"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

this is what Logcat says:

01-14 03:38:55.933: D/gralloc_goldfish(5610): Emulator without GPU emulation detected.
01-14 03:38:56.623: E/(5610): At line 8, column 71: not well-formed (invalid token)
01-14 03:38:56.633: D/AndroidRuntime(5610): Shutting down VM
01-14 03:38:56.633: W/dalvikvm(5610): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-14 03:38:56.653: E/AndroidRuntime(5610): FATAL EXCEPTION: main
01-14 03:38:56.653: E/AndroidRuntime(5610): java.lang.NullPointerException
01-14 03:38:56.653: E/AndroidRuntime(5610):     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at android.widget.ListView.setAdapter(ListView.java:462)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at net.con.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:75)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at net.con.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:1)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at android.os.AsyncTask.finish(AsyncTask.java:631)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at android.os.Looper.loop(Looper.java:137)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at java.lang.reflect.Method.invokeNative(Native Method)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at java.lang.reflect.Method.invoke(Method.java:511)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-14 03:38:56.653: E/AndroidRuntime(5610):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 161

Answers (2)

gaurav5430
gaurav5430

Reputation: 13892

your ArrayAdapter is giving a null Pointer Exception in setAdapter while trying to getCount(). This is most probably because you did not recieve any values in result in onPostExecute

EDIT:

onPostExecute uses the result returned from doInBackground, which, in your case returns null. this means result in onPostExecute is null , you build an adapter using this result, which is null , that is why you get a null pointer exception when you try to set an adapter with count = 0. This means that you are getting an exception in the try catch block and hence it returns null instead of returning rssReader.getItems();.

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

In your logcat, the following lines tell you what the error is

FATAL EXCEPTION: main
01-14 01:45:08.103: E/AndroidRuntime(3181): java.lang.NullPointerException

So its a NPE...something is null. The next line after that which references your project tells you where the exception occurs

at net.con.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:73)

So something is null at line 73 of MainActivity which is in onPostExecute() of GetRSSDataTask.

Now that we know how to find the exception and we know that line 73 is

itcItems.setAdapter(adapter);

so you need to see what is null and what could cause this exception. If itcItems isn't null, set a breakpoint and see, then maybe your adapter is null.

Now that you know how you can read the logcat this will help you to only post the most relevant code. The other classes and xml files aren't necessary in this case since the exception occurs in MainActivity. Posting only the most relevant code initially makes it easier to get help here.

Upvotes: 1

Related Questions