Problem regarding extending ListActivity
In my project whenever I extend activity it works fine but as soon as I extend ListActivity it throws exception and shows file not found. Why is that? We already know that ListActivity itself extends the Activity class. The application must run fine.
Here's the java file:
package com.android.feedGrabber;
import java.net.URL;
import ja开发者_如何学JAVAva.net.URLConnection;
import java.util.Collection;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.sun.cnpi.rss.elements.Item;
import com.sun.cnpi.rss.elements.Rss;
import com.sun.cnpi.rss.parser.RssParser;
import com.sun.cnpi.rss.parser.RssParserFactory;
public class feedGrabber extends Activity {
public static Collection<Item> readRSSDocument(String url) throws Exception {
RssParser parser = RssParserFactory.createDefault();
URL feedUrl = new URL(url);
URLConnection urlc=feedUrl.openConnection();
urlc.setRequestProperty("User-Agent","");
urlc.connect();
Rss rss = parser.parse(urlc.getInputStream());
return rss.getChannel().getItems();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int links=0;
try
{
for(Item item : feedGrabber.readRSSDocument("http://feeds.feedburner.com/Tutorialzine")){
if(links++>3)
break;
TextView t = (TextView)findViewById(R.id.title);
t.append("Title: ");
t.append(item.getTitle().toString());
t.append("\n\n");
//System.out.println("Title: " + item.getTitle());
//System.out.println("Link: " + item.getLink());
}
}catch(Exception e)
{
//
}
}
}
If I change "extends Activity" to "extends ListActivity" it gives the exception. I need to bring this change because I want to implement it on ListView instead of TextView.
Error it throws: debug mode gets active and it highlights under Suspended(exception RunTimeException): ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663.
When you extend a ListActivity, it is expecting the Layout to have a ListView that has an id of "@android:id/list", if you do not have it, it will throw an exception
You layout should look something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/noresults"/>
</LinearLayout>
Without seeing your main.xml or the exception I can't be sure, but I'd suggest checking if your list has the @android:id/list
identity as required.
精彩评论