代码获取类别数据,当我点击语言活动并将数据加载到另一个活动时,我试图显示这些数据,但似乎更难以理解的问题。
这是我在调用活动时遇到的问题
Process: com.status.funnystatus.wishes, PID: 19112
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.status.funnystatus.wishes.helper.CategoryAdapter.getItemCount(CategoryAdapter.java:159)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3603)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3103)
at android.view.View.measure(View.java:17474)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17474)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:786)
at android.view.View.measure(View.java:17474)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:17474)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17474)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17474)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17474)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5464)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17474)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5786)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
这是我遇到问题的文件
CategoryAdapter.java
package com.status.funnystatus.wishes.helper;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import com.status.funnystatus.wishes.R;
import com.status.funnystatus.wishes.models.Category;
import com.status.funnystatus.wishes.models.Subcategory;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import com.status.funnystatus.wishes.api.APIService;
import com.status.funnystatus.wishes.api.APIUrl;
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
private List<Category> categories;
private Context mCtx;
private OnItemClickListener mOnItemClickListener;
private Retrofit retrofit;
private APIService service;
public CategoryAdapter(List<Category> categories, Context mCtx) {
this.categories = categories;
this.mCtx = mCtx;
retrofit = new Retrofit.Builder()
.baseUrl(APIUrl.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(APIService.class);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_users, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(CategoryAdapter.ViewHolder holder, final int position) {
Category category = categories.get(position);
// holder.textViewName.setText(subcategory.getCat_id());
holder.textViewName.setText(category.getName());
}
@Override
public int getItemCount() {
return categories.size();
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public TextView textViewMessage;
public TextView textViewCount;
public ViewHolder(View itemView) {
super(itemView);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textViewMessage = (TextView) itemView.findViewById(R.id.textViewMessage);
textViewCount = (TextView) itemView.findViewById(R.id.textViewCount);
itemView.setOnClickListener(mOnClickListener);
}
View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v, getPosition());
}
};
}
}
答案 0 :(得分:0)
您需要初始化您的类别,在您的活动创建中,它将查询适配器大小。
private List<Category> categories = new List<Category>();
答案 1 :(得分:0)
由于您拨打setAdapter()
的时间,列表未初始化,因此getItemCount()
上的NullPointerException
结果List<Category>
试试这个:
@Override
public int getItemCount() {
return categories == null ? 0 : categories.size();
}