我正在尝试从另一个recyclerview适配器中填充recyclerview。 rv.setLayoutManager(new LinearLayoutManager(context))
代码在此行中断开控制台输出是错误的:
08-17 21:14:20.221 11999-11999/in.co.sd.app E/RecyclerView: No adapter attached; skipping layout
08-17 21:14:20.227 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@85153aa
08-17 21:14:20.228 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@19a9b9b
08-17 21:14:20.230 11999-11999/in.co.sd.app E/RecyclerView: No adapter attached; skipping layout
08-17 21:14:20.237 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@f1fab38
08-17 21:14:20.237 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@bf72711
08-17 21:14:20.239 11999-11999/in.co.sd.app E/RecyclerView: No adapter attached; skipping layout
08-17 21:14:20.244 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@e490076
08-17 21:14:20.244 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@47d5677
08-17 21:14:20.247 11999-11999/in.co.sd.app E/RecyclerView: No adapter attached; skipping layout
08-17 21:14:20.253 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@c042e4
08-17 21:14:20.253 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@696234d
08-17 21:14:20.255 11999-11999/in.co.sd.app E/RecyclerView: No adapter attached; skipping layout
08-17 21:14:20.260 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@73cae02
08-17 21:14:20.261 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@966e313
08-17 21:14:20.263 11999-11999/in.co.sd.app E/RecyclerView: No adapter attached; skipping layout
08-17 21:14:20.266 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@db0a950
08-17 21:14:20.266 11999-11999/in.co.sd.app V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@baea749
08-17 21:14:20.267 11999-11999/in.co.sd.app E/RecyclerView: No adapter attached; skipping layout
08-17 21:14:40.197 11999-11999/in.co.sd.app D/AndroidRuntime: Shutting down VM
--------- beginning of crash
08-17 21:14:40.198 11999-11999/in.co.sd.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.co.servicedeal.app, PID: 11999
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at in.co.servicedeal.app.adapter.BrandAdapter$1.onClick(BrandAdapter.java:106)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21177)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
--------- beginning of system
08-17 21:14:40.201 11999-11999/in.co.sd.app E/MQSEventManagerDelegate: failed to get MQSService.
08-17 21:14:40.242 11999-11999/in.co.sd.app I/Process: Sending signal. PID: 11999 SIG: 9
适配器代码
public class BrandAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private Context context;
private ArrayList<BrandBean> brandList;
private RecyclerView rv;
public BrandAdapter(final Context context, final ArrayList<BrandBean> brandList) {
this.context = context;
this.brandList = brandList;
}
@Override
public int getCount() {
return brandList.size();
}
@Override
public Object getItem(int i) {
return brandList.get(i).getId();
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = layoutInflater.inflate(R.layout.item_brand_service_grid, null);
//Grid Text
final TextView gridText = view.findViewById(R.id.brand_grid_text);
gridText.setText(brandList.get(i).getServiceType());
//Grid Arraw
final ImageView gridArrow = view.findViewById(R.id.brand_grid_arrow);
gridArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Grid Dropdown
rv = view.findViewById(R.id.recycler_view_SI);
rv.setLayoutManager(new LinearLayoutManager(context));
getProductDetailsByProductId(brandList.get(i).getId());
}
});
}
return view;
}
我们的想法是实现这种布局:
空白区域中将包含recyclerView内容。
答案 0 :(得分:1)
您正在获取空指针异常,因为Onclick方法中的视图名称与getView方法中的视图名称相同,因此视图的onclick方法会覆盖您的父视图,这就是您收到错误的原因。 要避免空指针异常,只需更改onclick视图名称,即
view1.gridArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View **view1**) {
//Grid Dropdown
rv = view.findViewById(R.id.recycler_view_SI);
rv.setLayoutManager(new LinearLayoutManager(context));
getProductDetailsByProductId(brandList.get(i).getId());
}
});
建议:您可以根据需要使用ExpandableListView,它易于管理。