我正在尝试使用feedparser库使用RssReader。它在Android M& N但它在版本L及以下崩溃。 这就是我所做的
ReadRss readRss = new ReadRss(this, recyclerView);
isNetworkAvailable();
if (isNetworkAvailable()) {
readRss.execute();
} else {
Toast.makeText(getBaseContext(), "Couldn't connect to Internet.", Toast.LENGTH_LONG).show();
}
ReadRss如下
class ReadRss extends AsyncTask<Void, Void, Void> {
Context context;
private ProgressDialog progressDialog;
private ArrayList<FeedItem> feedItems;
private RecyclerView recyclerView;
ReadRss(Context context, RecyclerView recyclerView) {
this.recyclerView = recyclerView;
this.context = context;
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading...");
}
@Override
protected void onPreExecute() {
progressDialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
RssAdapter adapter = new RssAdapter(context, feedItems);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.addItemDecoration(new VerticalSpace(50));
recyclerView.setAdapter(adapter);
}
@Override
protected Void doInBackground(Void... params) {
ProcessXml(Getdata());
return null;
}
private void ProcessXml(Document data) {
try {
URL url = new URL("http:pockettanksnews.blogspot.com/feeds/posts/default");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream feedStream = httpConnection.getInputStream();
FeedParser parser = FeedParserFactory.newParser();
System.out.println("success");
Feed feed = parser.parse(feedStream);
feedItems=new ArrayList<>();
for (Item i : feed.getItemList()) {
FeedItem item=new FeedItem();
System.out.println(i.getTitle());
item.setTitle(i.getTitle());
System.out.println(i.getLink());
item.setLink(i.getLink());
System.out.println(i.getPubDate());
item.setPubDate(i.getPubDate().toString());
item.setThumbnailUrl("https:1079638729.rsc.cdn77.org/androidgame_img/pocket_tanks/real/1_pocket_tanks.jpg");
item.setDescription(i.getDescription());
feedItems.add(item);
}
} else {
System.out.println("error");
}
}
catch (Exception except){
;
}
}
private Document Getdata() {
try {
String address = "http://pockettanksnews.blogspot.com/feeds/posts/default/-/pttipsyoutube/?alt=rss";
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDoc = builder.parse(inputStream);
return xmlDoc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
我的RssAdapter如下
public class RssAdapter extends RecyclerView.Adapter<RssAdapter.MyViewHolder> {
ArrayList<FeedItem> feedItems=new ArrayList<FeedItem>();
Context context;
public RssAdapter(Context context, ArrayList<FeedItem> feedItems) {
this.feedItems = feedItems;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custum_row_news_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
FeedItem current = feedItems.get(position);
holder.Title.setText(current.getTitle());
holder.Description.loadDataWithBaseURL("", current.getDescription(), "text/html", "UTF-8", "");
holder.Date.setText(current.getPubDate());
}
@Override
public int getItemCount() {
return feedItems.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView Title, Date;
WebView Description;
ImageView Thumbnail;
CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
Title = (TextView) itemView.findViewById(R.id.title_text);
Description = (WebView) itemView.findViewById(R.id.description_text);
Description.getSettings().setJavaScriptEnabled(true);
Date = (TextView) itemView.findViewById(R.id.date_text);
Thumbnail = (ImageView) itemView.findViewById(R.id.thumb_img);
cardView = (CardView) itemView.findViewById(R.id.cardview);
}
}
应用程序在getItemCount方法崩溃。它显示Null Pointer Exception:尝试调用虚方法&#39; int java.util.ArrayList.size()&#39;在null对象引用上。
答案 0 :(得分:0)
如果出现错误,您的Feed将变为空。所以改变你的代码并处理它。
// Add null check here.
if(feedItems!= null) {
RssAdapter adapter = new RssAdapter(context, feedItems);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.addItemDecoration(new VerticalSpace(50));
recyclerView.setAdapter(adapter);
} else {
// Show some error
}
处理此空指针的另一种方法是在Adapter内添加null check。
public RssAdapter(Context context, ArrayList<FeedItem> feedItems) {
if(feedItems!= null) {
this.feedItems.addAll(feedItems);
}
this.context = context;
}
然后尝试从服务器获取数据时找出异常。我相信只有 Getdata()方法负责。