我是新手,仍然没有完全掌握这个适配器。从错误看来,因为它的null我的猜测是我可能必须初始化列表。问题是我对如何以及为何从api获取列表感到困惑。如果有人能指出我正确的方向,将非常感激
如果您需要更多信息,请告知我,以便我提供
这是我的主要活动
public class KeanTrolley extends AppCompatActivity {
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kean_trolley);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().hide();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.colorPrimary)));
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "COMING SOON", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
mRecyclerView = (RecyclerView)findViewById(R.id.rv);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
StationService stationService = StationServiceProvider.createService(StationService.class);
Call<List<stations>> call = stationService.listStations();
call.enqueue(new Callback<List<stations>>() {
@Override
public void onResponse(Response<List<stations>> response, Retrofit retrofit) {
mAdapter = new StationsAdapter(response.body());
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onFailure(Throwable t) {
}
});
}
这是我的适配器类
public class StationsAdapter extends RecyclerView.Adapter<StationsAdapter.ViewHolder> {
public Context context;
private List<stations> mStations;
//Context mContext;
public StationsAdapter(List<stations> station) {
mStations = station;
//mContext = context;
}
@Override
public StationsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rv_card_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(StationsAdapter.ViewHolder holder, int position) {
stations stationLoc = mStations.get(position);
holder.TvStationName.setText(stationLoc.getmStationName());
holder.IvStationPic.setImageResource(R.drawable.kean_logo_1);
holder.mCardView.setTag(position);
}
@Override
public int getItemCount() {
return mStations.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView TvStationName;
public ImageView IvStationPic;
public View mCardView;
public ViewHolder(View itemView) {
super(itemView);
TvStationName = (TextView) itemView.findViewById(R.id.station_name);
IvStationPic = (ImageView) itemView.findViewById(R.id.station_photo);
mCardView = (CardView) itemView.findViewById(R.id.cv);
mCardView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = (int) v.getTag();
Intent intent = new Intent(v.getContext(), Trolley_Activity.class);
v.getContext().startActivity(intent);
//Toast.makeText(v.getContext(), Integer.toString(position), Toast.LENGTH_SHORT).show();
}
}
}
这是错误
11-20 21:25:58.688 18393-18393/com.example.ogure.keantransport E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ogure.keantransport, PID: 18393
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.ogure.keantransport.Adapter.StationsAdapter.getItemCount(StationsAdapter.java:56)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2556)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:78)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1109)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:675)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
尝试将其更改为..
@Override
public int getItemCount() {
return null!=mStations?mStations.size():0;
}
我相信你会得到空名单。'mStations'。
您应该检查设置适配器的列表(StationsAdapter)。可能是您将空列表作为参数传递
编辑:
@Override
public void onResponse(Response<List<stations>> response, Retrofit retrofit) {
if(response.body()==null){
// show some error message
}else{
mAdapter = new StationsAdapter(response.body());
mRecyclerView.setAdapter(mAdapter);
}
}
答案 1 :(得分:0)
请检查此部分中的代码: -
{{1}}
在onResponse()方法中,您将response.body()作为工作站列表传递。那个response.body()方法可能会返回一个导致错误的空列表。
答案 2 :(得分:0)
就我而言,我正在这样做:
public FavQuotesCards(ArrayList<Quote> quotes) {
quotes = quotes;
}
代替:
public FavQuotesCards(ArrayList<Quote> quotes) {
this.quotes = quotes;
}
你相信吗?