我正在尝试使用许多“ grid_cell” xml布局在回收者视图中设置gridLayout,每个布局均包含5个ImageView。在重新构造代码以容纳数据库之前,我使recyclerView正常工作。
问题是在MapViewHolder构造函数中,所有ImageView都被初始化为null,导致在空对象引用上出现java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)
”
public class MapFragment extends Fragment
{
private GameDataStorage gameDataStorage;
private RecyclerView rv;
private MapAdapter adapter;
private GridLayoutManager gridLayoutManager;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gameDataStorage = ((SubApplication)getActivity().getApplication()).gameDataStorage;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle)
{
//Inflate the view from fragment_map.xml layout.
View view = inflater.inflate(R.layout.fragment_map, parent, false);
//Crate the recycler view from the map_recycler_view.xml layout
rv = view.findViewById(R.id.map_recycler_view);
adapter = new MapAdapter();
//Initialise the grid layout manager
gridLayoutManager = new GridLayoutManager(getActivity(), GameData.getInstance().getSettings().getMapHeight(), GridLayoutManager.HORIZONTAL, false);
//Set the recycler view adapter
rv.setAdapter(adapter);
//Set the layout manager to be a horizontal scrolling grid
rv.setLayoutManager(gridLayoutManager);
return view;
}
public class MapViewHolder extends RecyclerView.ViewHolder
{
private ImageView structure;
private ImageView topLeft;
private ImageView topRight;
private ImageView bottomLeft;
private ImageView bottomRight;
public MapViewHolder(LayoutInflater li, ViewGroup parent)
{
super(li.inflate(R.layout.grid_cell, parent, false));
int size = parent.getMeasuredHeight() / GameData.getInstance().getSettings().getMapHeight() + 1;
ViewGroup.LayoutParams lp = parent.getLayoutParams();
lp.height = size;
lp.width = size;
structure = (ImageView) parent.findViewById(R.id.structure);
topLeft = (ImageView) parent.findViewById(R.id.top_left);
topRight = (ImageView) parent.findViewById(R.id.top_right);
bottomLeft = (ImageView) parent.findViewById(R.id.bottom_left);
bottomRight = (ImageView) parent.findViewById(R.id.bottom_right);
//DEBUGGING
Log.d("GAME_DATA", "structure reference is initialised as " + structure);
Log.d("GAME_DATA", "topLeft reference is initialised as " + topLeft);
Log.d("GAME_DATA", "topRight reference is initialised as " + topRight);
Log.d("GAME_DATA", "bottomLeft reference is initialised as " + bottomLeft);
Log.d("GAME_DATA", "bottomRight reference is initialised as " + bottomRight);
}
public void bind(final MapElement element, final int index)
{
if(element.getStructure() != null){
structure.setImageResource(element.getStructure().getImageId());
}
else
{
if(structure == null)
{
Log.d("GAME_DATA", "structure reference is null for mapElement " + index);
}
structure.setImageResource(GameData.getInstance().getEmpty());
}
topLeft.setImageResource(element.getTopLeft());
topRight.setImageResource(element.getTopRight());
bottomLeft.setImageResource(element.getBottomLeft());
bottomRight.setImageResource(element.getBottomRight());
structure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
int row = index % GameData.getInstance().getSettings().getMapHeight();
int col = index / GameData.getInstance().getSettings().getMapHeight();
StructureData.setStructureAt(row, col);
gameDataStorage.updateMapElement(element, element.getStructure(), index);
adapter.notifyItemChanged(index);
}
});
structure.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
});
}
}
// END_INCLUDE(recyclerViewSampleViewHolder)
public class MapAdapter extends RecyclerView.Adapter<MapViewHolder>
{
// BEGIN_INCLUDE(recyclerViewOnCreateViewHolder)
// Create new views (invoked by the layout manager)
@Override
public MapViewHolder onCreateViewHolder(ViewGroup container, int viewType)
{
Log.d("GAME_DATA", "new MapViewHolder using ViewGroup " + container + " and viewType " + viewType);
return new MapViewHolder(LayoutInflater.from(getActivity()), container);
}
// END_INCLUDE(recyclerViewOnCreateViewHolder)
// BEGIN_INCLUDE(recyclerViewOnBindViewHolder)
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MapViewHolder vh, int index) {
// Get element from your dataset at this position and replace the contents of the view
// with that element
int row = index % GameData.getInstance().getSettings().getMapHeight();
int col = index / GameData.getInstance().getSettings().getMapHeight();
MapElement element = GameData.getInstance().getMapLoc(row, col);
vh.bind(element, index);
}
// END_INCLUDE(recyclerViewOnBindViewHolder)
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
int rows = GameData.getInstance().getMap().length;
int cols = GameData.getInstance().getMap()[0].length;
int length = rows * cols;
Log.d("GAME_DATA", "getItemCount returns: " + length);
return length;
}
}
}
Logcat输出:
10-22 19:31:59.177 19926-19926 / com.example.mad_assignment_rebuild D / GAME_DATA:使用ViewGroup androidx.recyclerview.widget.RecyclerView {353fb6f6 VFED .......... ID 0,0-的新MapViewHolder 1080,1188#7f07005e app:id / map_recycler_view}和viewType 0
10-22 19:31:59.187 19926-19926 / com.example.mad_assignment_rebuild D / GAME_DATA:结构引用初始化为null
10-22 19:31:59.187 19926-19926 / com.example.mad_assignment_rebuild D / GAME_DATA:topLeft引用初始化为null
10-22 19:31:59.187 19926-19926 / com.example.mad_assignment_rebuild D / GAME_DATA:topRight引用被初始化为null
10-22 19:31:59.187 19926-19926 / com.example.mad_assignment_rebuild D / GAME_DATA:bottomLeft引用初始化为null
10-22 19:31:59.187 19926-19926 / com.example.mad_assignment_rebuild D / GAME_DATA:bottomRight引用被初始化为null
10-22 19:31:59.187 19926-19926 / com.example.mad_assignment_rebuild D / GAME_DATA:mapElement 0的结构引用为空
调试错误输出:
E / AndroidRuntime:致命异常:main
流程:com.example.mad_assignment_rebuild,PID:19926
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.ImageView.setImageResource(int)”
在com.example.mad_assignment_rebuild.MapRecyclerFiles.MapFragment $ MapViewHolder.bind(MapFragment.java:101)
在com.example.mad_assignment_rebuild.MapRecyclerFiles.MapFragment $ MapAdapter.onBindViewHolder(MapFragment.java:154)
在com.example.mad_assignment_rebuild.MapRecyclerFiles.MapFragment $ MapAdapter.onBindViewHolder(MapFragment.java:132)
最佳答案
我认为有一个问题是您想从parent
视图中找到视图。您应该从item layout
视图(在本例中为R.layout.grid_cell
)中找到视图。
尝试这样。
public MapViewHolder(LayoutInflater li, ViewGroup parent)
{
final View = li.inflate(R.layout.grid_cell, parent, false);
super(view);
int size = view.getMeasuredHeight() / GameData.getInstance().getSettings().getMapHeight() + 1;
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.height = size;
lp.width = size;
structure = (ImageView) view.findViewById(R.id.structure);
topLeft = (ImageView) view.findViewById(R.id.top_left);
topRight = (ImageView) view.findViewById(R.id.top_right);
bottomLeft = (ImageView) view.findViewById(R.id.bottom_left);
bottomRight = (ImageView) view.findViewById(R.id.bottom_right);
//DEBUGGING
Log.d("GAME_DATA", "structure reference is initialised as " + structure);
Log.d("GAME_DATA", "topLeft reference is initialised as " + topLeft);
Log.d("GAME_DATA", "topRight reference is initialised as " + topRight);
Log.d("GAME_DATA", "bottomLeft reference is initialised as " + bottomLeft);
Log.d("GAME_DATA", "bottomRight reference is initialised as " + bottomRight);
}
或者,您可以做得更清楚和简单,
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Holder> {
private final List<Model> list;
RecyclerViewAdapter(List<WorkshopObj> data, EditTextChangeWorkshopListener editTextChangeWorkshopListener) {
list = data;
setHasStableIds(true);
}
@NotNull
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_workshop_adapter, parent, false));
}
@Override
public void onBindViewHolder(final Holder holder, final int position) {
final WorkshopObj model = list.get(position);
holder.titleWorkshop.setText(model.getTitleText());
holder.requiredWorkshop.setVisibility(model.isRequired() ? View.VISIBLE : View.GONE);
holder.subTitle.setVisibility(model.isMandatory() ? View.VISIBLE : View.GONE);
}
@Override
public int getItemCount() {
return list.size();
}
class Holder extends RecyclerView.ViewHolder {
private final TextView requiredWorkshop;
private final TextView titleWorkshop;
private final TextView subTitle;
private final EditText price;
Holder(View view) {
super(view);
requiredWorkshop = view.findViewById(R.id.required_workshop);
titleWorkshop = view.findViewById(R.id.title_workshop);
subTitle = view.findViewById(R.id.sub_title);
price = view.findViewById(R.id.price);
}
}
}