简体   繁体   中英

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 java.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>

没有看到您的main.xml或例外,我不能肯定,但我建议你检查,如果您的列表具有@android:id/list身份要求

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM