0

我正在尝试使用ROME Feed Reader在 android 4.1 上制作新闻阅读器。但是,当我的应用程序尝试调用新闻源时,我不断收到错误消息。我一直忙于实现 Asynctask,因为 4.1 不允许我在主线程上检索 rss-feeds。

ERROR/AndroidRuntime(27669): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.cloud.backend.android/com.google.cloud.backend.android.FeedActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.access$700(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.google.cloud.backend.android.FeedListAdapter.getCount(FeedListAdapter.java:32)
        at android.widget.ListView.setAdapter(ListView.java:466)
        at com.google.cloud.backend.android.FeedActivity.createList(FeedActivity.java:34)
        at com.google.cloud.backend.android.FeedActivity.onCreate(FeedActivity.java:17)
        at android.app.Activity.performCreate(Activity.java:5206)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)

这是我的代码(部分):

public class FeedListAdapter extends BaseAdapter
{
    private SyndFeed feed;
    private Activity context;

    public FeedListAdapter( Activity context )
    {
        this.context = context;
        String feedUrl = context.getIntent().getExtras().getString("Feed");
        new FeedRetrieval().execute(feedUrl);
    }

    public int getCount()
    {
        return feed.getEntries().size();
    }

    ... (shortened)

    private class FeedRetrieval extends AsyncTask<String, Integer, String>
    {
        @Override
        protected String doInBackground(String... params) {
            RssAtomFeedRetriever feedRetriever = new RssAtomFeedRetriever();
            feed = feedRetriever.getMostRecentNews(params[0]);
            return null;
        }
    }

编辑:我已将 getCount() 更改为:

if (feed != null) {
    return feed.getEntries().size();
}
return 0;

但现在我得到:

07-28 01:14:34.182: ERROR/AndroidRuntime(29175): FATAL EXCEPTION: AsyncTask #1
07-28 01:14:34.242: ERROR/android.os.Debug(433): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

Logcat 没有显示导致 AsyncTask 失败的原因。这对我来说更没有意义:(

4

2 回答 2

0

你的feed变量是null直到AsyncTask完成执行。如果你getCount在它完成之前调用它,它会尝试访问feed,也就是null那个时候。

返回默认值或提供一种方法来检查数据是否实际存在。

于 2013-07-27T23:05:41.233 回答
0

解决方案:我知道了。该示例托管在我的 GitHub 上:https ://github.com/praetore/AndroidROMEFeedReader

于 2013-07-29T01:32:46.980 回答