简体   繁体   English

有关扩展ListActivity的问题

[英]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. 在我的项目中,每当我扩展活动时,它都可以正常工作,但只要我扩展ListActivity,就会抛出异常并显示找不到的文件。 Why is that? 这是为什么? We already know that ListActivity itself extends the Activity class. 我们已经知道ListActivity本身扩展了Activity类。 The application must run fine. 应用程序必须正常运行。

Here's the java file: 这是java文件:

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. 如果我将“extends Activity”更改为“extends ListActivity”,则会给出异常。 I need to bring this change because I want to implement it on ListView instead of TextView. 我需要进行此更改,因为我想在ListView而不是TextView上实现它。

Error it throws: debug mode gets active and it highlights under Suspended(exception RunTimeException): ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663. 抛出错误:调试模式变为活动状态,并在Suspended(异常RunTimeException)下突出显示:ActivityThread.performLaunchActivity(ActivityThread $ ActivityRecord,Intent)行: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 当您扩展ListActivity时,它期望Layout具有ID为“@android:id / list”的ListView,如果您没有它,它将抛出异常

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身份要求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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