我使用RecyclerView
来显示来自各种RSS站点的RSS源。当应用程序加载和设备离线时,应用程序崩溃并出现以下错误
at com.nepalpolice.sample.alarm.data.Adapter.FeedAdapter.getItemCount(FeedAdapter.java:104)
但是,如果应用程序是在线的,然后应用程序加载顺利,下次设备离线,然后应用程序也工作.但只有当第一次应用程序加载和设备离线,然后只有应用程序崩溃....当我点击日志错误....它引导我到下面的行
@Override
public int getItemCount() {
return rssObject.items.size();
}
}
下面是类的全部代码
class FeedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener
{
public TextView txtTitle,txtPubDate,txtContent;
private ItemClickListener itemClickListener;
public FeedViewHolder(View itemView) {
super(itemView);
txtTitle = (TextView)itemView.findViewById(R.id.txtTitle);
txtPubDate = (TextView)itemView.findViewById(R.id.txtPubDate);
txtContent = (TextView)itemView.findViewById(R.id.txtContent);
//Set Event
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),false);
}
@Override
public boolean onLongClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),true);
return true;
}
}
public class FeedAdapter extends RecyclerView.Adapter<FeedViewHolder> {
private RSSObject rssObject;
private Context mContext;
private LayoutInflater inflater;
public FeedAdapter(RSSObject rssObject, Context mContext) {
this.rssObject = rssObject;
this.mContext = mContext;
inflater = LayoutInflater.from(mContext);
}
@Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = inflater.inflate(R.layout.row,parent,false);
return new FeedViewHolder(itemView);
}
@Override
public void onBindViewHolder(FeedViewHolder holder, int position) {
holder.txtTitle.setText(rssObject.getItems().get(position).getTitle());
holder.txtPubDate.setText(rssObject.getItems().get(position).getPubDate());
holder.txtContent.setText(rssObject.getItems().get(position).getContent());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
if(!isLongClick)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(rssObject.getItems().get(position).getLink()));
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(browserIntent);
}
}
});
}
@Override
public int getItemCount() {
return rssObject.items.size();
}
}
下面是主要的片段代码
public class Movie extends Fragment {
RecyclerView recyclerView;
RSSObject rssObject;
private final String RSS_link="http://www.bollywoodhungama.com/rss/movie-release-date.xml";
private final String RSS_to_Json_API = "https://api.rss2json.com/v1/api.json?rss_url=";
public Movie() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_mainrss, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext(),LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
loadRSS();
return view;
}
private void loadRSS() {
AsyncTask<String,String,String> loadRSSAsync = new AsyncTask<String, String, String>() {
ProgressDialog mDialog = new ProgressDialog(getActivity());
@Override
protected void onPreExecute() {
mDialog.setMessage("Please wait...");
mDialog.show();
}
@Override
protected String doInBackground(String... params) {
String result;
HTTPDataHandler http = new HTTPDataHandler();
result = http.GetHTTPData(params[0]);
return result;
}
@Override
protected void onPostExecute(String s) {
mDialog.dismiss();
rssObject = new Gson().fromJson(s,RSSObject.class);
FeedAdapter adapter = new FeedAdapter(rssObject,getActivity().getBaseContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
};
StringBuilder url_get_data = new StringBuilder(RSS_to_Json_API);
url_get_data.append(RSS_link);
loadRSSAsync.execute(url_get_data.toString());
}
}
有没有人可以帮助我这个错误是什么以及如何解决这个问题?
2条答案
按热度按时间hmae6n7t1#
看起来你正在创建一个
RSSObject
的对象,但是它里面的列表将被初始化为null
,所以为了避免这种使用kyks70gy2#
要显示rssObject是否为null,请执行以下操作:
试试看有没有用。