错误日志 -
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at in.hareshsaliya.itijobs.Adapter.ListSourceAdapter.getItemCount(ListSourceAdapter.java:115)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3603)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3408)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
at android.view.View.layout(View.java:17953)
at android.view.ViewGroup.layout(ViewGroup.java:5717)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:610)
at android.view.View.layout(View.java:17953)
at android.view.ViewGroup.layout(ViewGroup.java:5717)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at android.view.View.layout(View.java:17953)
at android.view.ViewGroup.layout(ViewGroup.java:5717)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
at android.view.View.layout(View.java:17953)
at android.view.ViewGroup.layout(ViewGroup.java:5717)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at android.view.View.layout(View.java:17953)
at android.view.ViewGroup.layout(ViewGroup.java:5717)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
at android.view.View.layout(View.java:17953)
at android.view.ViewGroup.layout(ViewGroup.java:5717)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
at android.view.View.layout(View.java:17953)
at android.view.ViewGroup.layout(ViewGroup.java:5717)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2715)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2383)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1470)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7001)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:910)
at android.view.Choreographer.doCallbacks(Choreographer.java:712)
at android.view.Choreographer.doFrame(Choreographer.java:646)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:896)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6293)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
代码 -
这是我的适配器代码**
@Override
public int getItemCount() {
return webSite.getSources().size();
}
这是问题**
class ListSourceViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener
{
ItemClickListener itemClickListener;
TextView source_title ;
CircleImageView source_image;
public ListSourceViewHolder(View itemView) {
super(itemView);
source_image = (CircleImageView) itemView.findViewById(R.id.source_image);
source_title = (TextView) itemView.findViewById(R.id.source_name);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View view) {
itemClickListener.onClick(view,getAdapterPosition(),false);
}
}
public class ListSourceAdapter extends RecyclerView.Adapter<ListSourceViewHolder> {
private Context context;
private WebSite webSite;
private IconBetterIdeaService mService;
public ListSourceAdapter(Context context, WebSite webSite) {
this.context = context;
this.webSite = webSite;
mService = Common.getIconService();
}
@Override
public ListSourceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.source_layout,parent,false);
return new ListSourceViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ListSourceViewHolder holder, int position) {
StringBuilder iconBetterAPI = new StringBuilder("https://icons.better-idea.org/allicons.json?url=");
iconBetterAPI.append(webSite.getSources().get(position).getUrl());
mService.getIconUrl(iconBetterAPI.toString())
.enqueue(new Callback<IconBetterIdea>() {
@Override
public void onResponse(Call<IconBetterIdea> call, Response<IconBetterIdea> response) {
if (response.body().getIcons().size() > 0)
{
Picasso.with(context)
.load(response.body().getIcons().get(0).getUrl())
.into(holder.source_image);
}
}
@Override
public void onFailure(Call<IconBetterIdea> call, Throwable t) {
}
});
holder.source_title.setText(webSite.getSources().get(position).getName());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int postion, boolean isLongClick) {
}
});
}
@Override
public int getItemCount() {
return webSite.getSources().size();
}
}
初始化并调用适配器这里
`
RecyclerView listWebsite;
RecyclerView.LayoutManager layoutManager;
NewsService mService;
ListSourceAdapter adapter;
android.app.AlertDialog dialog;
SwipeRefreshLayout swipeRefreshLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
Paper.init(this);
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeRefresh);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadWebsiteSource(true);
}
});
mService = Common.getNewsService();
listWebsite = (RecyclerView)findViewById(R.id.list_source);
listWebsite.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
listWebsite.setLayoutManager(layoutManager);
dialog = new SpotsDialog(this);
loadWebsiteSource(false);
}
private void loadWebsiteSource(boolean isRefreshed) {
if (!isRefreshed)
{
String cache = Paper.book().read("cache");
if (cache != null && !cache.isEmpty())
{
WebSite webSite = new Gson().fromJson(cache, WebSite.class);
adapter = new ListSourceAdapter(getBaseContext(),webSite);
adapter.notifyDataSetChanged();
listWebsite.setAdapter(adapter);
} else
{
dialog.show();
mService.getSources().enqueue(new Callback<WebSite>() {
@Override
public void onResponse(Call<WebSite> call, Response<WebSite> response) {
adapter = new ListSourceAdapter(getBaseContext(),response.body());
adapter.notifyDataSetChanged();
listWebsite.setAdapter(adapter);
Paper.book().write("cache",new Gson().toJson(response.body()));
}
@Override
public void onFailure(Call<WebSite> call, Throwable t) {
}
});
}
}
else
{
dialog.show();
mService.getSources().enqueue(new Callback<WebSite>() {
@Override
public void onResponse(Call<WebSite> call, Response<WebSite> response) {
adapter = new ListSourceAdapter(getBaseContext(),response.body());
adapter.notifyDataSetChanged();
listWebsite.setAdapter(adapter);
Paper.book().write("cache",new Gson().toJson(response.body()));
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onFailure(Call<WebSite> call, Throwable t) {
}
});
}
}
`
答案 0 :(得分:0)
使用初始化创建List引用,如下所示
List list = new ArrayList();
如果要更新现有版本,请使用以下
list.clear();
list.addAll(newList);
多数民众赞成。如果不修复,请先发布您的代码。
答案 1 :(得分:0)
您没有将数组列表传递给此Adapter类。您如何期望它不会给出空指针异常。
将数组列表初始化为 - :
ArrayList list=new ArrayList();
public ListSourceAdapter(Context context, ArrayList list) {
this.context = context;
this.list = list;
mService = Common.getIconService();
}
如果是recycleler视图,请将此适配器调用为 - :
adapter=new ListSourceAdapter(context,list);
recyclerview.setAdapter(adapter);