我试图使用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失败的原因。这对我来说更不合理:(
答案 0 :(得分:0)
feed
变量为null
,直到AsyncTask
完成执行。如果您在完成之前致电getCount
,它会尝试访问feed
,当时为null
。
返回默认值或提供检查数据是否实际存在的方法。
答案 1 :(得分:0)
解决方案:我知道了。该示例托管在我的GitHub上:https://github.com/praetore/AndroidROMEFeedReader