我正在使用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());
}
}
任何人都可以帮助我解决此错误以及如何解决此问题吗?
答案 0 :(得分:2)
好像您正在创建RSSObject
的对象,但是其中的列表将被初始化为null
,以避免这种使用
@Override
public int getItemCount() {
return (rssObject!=null && rssObject.items!=null)? rssObject.items.size():0;
// if object and list is not null then return size otherwise return 0
}
答案 1 :(得分:0)
显示rssObject是否为空:
@Override
public int getItemCount() {
if (rssObject == null) {
Toast.makeText(context, "rssObject is NUll", Toast.LENGTH_SHORT).show();
return 0;
}
else {
return (rssObject!=null && rssObject.items!=null)? rssObject.items.size():0;
}
}
尝试一下是否有效。
答案 2 :(得分:0)
@Override
public int getItemCount() {
if (rssObject == null) {
Toast.makeText(context, "rssObject is NUll", Toast.LENGTH_SHORT).show();
return 0;
} else {
return (rssObject != null && rssObject.items != null) ?
rssObject.items.size() : 0;
}
}