我想做双循环视图来代表每层的房间。但是nullpointerexception发生在FlooradPter中。
public class FloorAdapter extends RecyclerView.Adapter<FloorAdapter.ViewHolder> {
public ArrayList<FloorData> floors;
private Context context;
private LayoutInflater layoutInflater;
public FloorAdapter(ArrayList<FloorData> floors, Context context) {
this.floors = floors;
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.signle_floor, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.recyclerView.setAdapter(new RoomAdapter(context, floors.get(position).rooms));
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setHasFixedSize(true);
holder.tvFloorNum.setText(floors.get(position).floorNum);
}
@Override
public int getItemCount() {
return floors.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
RecyclerView recyclerView;
TextView tvFloorNum;
public ViewHolder(View itemView) {
super(itemView);
recyclerView = (RecyclerView) itemView.findViewById(R.id.rvFloors);
tvFloorNum = (TextView) itemView.findViewById(R.id.tvFloorNum);
}
}
}
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.CustomViewHolder> {
private Context context;
private ArrayList<RoomData> rooms;
private LayoutInflater inflater;
public RoomAdapter(Context context, ArrayList<RoomData> rooms) {
this.context = context;
this.rooms = rooms;
this.inflater = LayoutInflater.from(context);
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = inflater.inflate(R.layout.single_room, parent, false);
return new CustomViewHolder(view);
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
RoomData room = rooms.get(position);
holder.tvRoomNum.setText(room.roomNum);
}
@Override
public int getItemCount() {
return rooms.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView tvRoomNum;
public CustomViewHolder(View itemView) {
super(itemView);
tvRoomNum = (TextView) itemView.findViewById(R.id.tvRoomNumber);
}
}
}
public class RoomActivity extends AppCompatActivity {
private RecyclerView rvFloor;
private FloorAdapter floorAdapter;
private ArrayList<FloorData> floors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room);
floors = prepareData();
rvFloor = findViewById(R.id.rvFloors);
floorAdapter = new FloorAdapter(floors, RoomActivity.this);
LinearLayoutManager manager = new LinearLayoutManager(RoomActivity.this);
rvFloor.setLayoutManager(manager);
rvFloor.setAdapter(floorAdapter);
}
private ArrayList<FloorData> prepareData() {
ArrayList<FloorData> floors = new ArrayList<FloorData>();
//첫번째 subject 추가
FloorData floor1 = new FloorData();
floor1.floorNum = 1;
floor1.rooms = new ArrayList<RoomData>();
RoomData room101 = new RoomData();
room101.roomNum = 101;
RoomData room102 = new RoomData();
room102.roomNum = 102;
RoomData room103 = new RoomData();
room103.roomNum = 103;
floor1.rooms.add(room101);
floor1.rooms.add(room102);
floor1.rooms.add(room103);
floors.add(floor1);
FloorData floor2 = new FloorData();
floor2.floorNum = 2;
floor2.rooms = new ArrayList<RoomData>();
RoomData room201 = new RoomData();
room201.roomNum = 201;
RoomData room202 = new RoomData();
room202.roomNum = 202;
RoomData room203 = new RoomData();
room203.roomNum = 203;
floor2.rooms.add(room201);
floor2.rooms.add(room202);
floor2.rooms.add(room203);
floors.add(floor2);
return floors;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tools.RewardActivity"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar_room"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="60dp"
android:background="@color/colorPrimaryDark"
app:title="호실 등록"
android:theme="@style/ToolbarTheme" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvFloors"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp">
<TextView
android:id="@+id/tvFloorNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textSize="30dp"
android:textColor="#000000" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvRooms"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp">
<TextView
android:id="@+id/tvRoomNumber"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:singleLine="true"
android:background="@color/colorPrimary"
/>
</LinearLayout>
</LinearLayout>
logcat解释
“java.lang.nullpointerexception:尝试对空对象引用调用虚拟方法”“void androidx.recyclerview.widget.recyclerview.setadapter(androidx.recyclerview.widget.recyclerview$adapter)””
我指的是https://medium.com/@ashishkudale/android-list-inside-list-using-recyclerview-73cff2c4ea95。几乎一样,但我不知道为什么会出错。请帮帮我
1条答案
按热度按时间vngu2lb81#
两件事。首先,在flooradapter的viewholder的构造函数中,找到的是activity的recycler视图,而不是adapter的视图。将代码更改为,
在地板上。这将解决您的崩溃。
但你在这里还会经历两次车祸
和
这是因为settext方法将字符串作为参数,但您传递的是integer或int。
所以把这两行都改成
holder.tvFloorNum.setText(String.valueOf(floors.get(position).floorNum));
holder.tvRoomNum.setText(String.valueOf(room.roomNum));
希望这个答案有用。