我知道此异常指出在imageLens管道的某些部分中存在一个空对象,但无法查明该对象。它向第48行和setImageResource方法发出信号,但仍无法修复。还查看了其他具有类似问题的问题,无法将其应用于此特定代码。
这是错误的活动
package com.komorebiestudio.cam_report_funcionality;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class LensAdapter extends RecyclerView.Adapter<LensAdapter.LensViewHolder> {
private ArrayList<LensItem> mLensList;
public static class LensViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageLens;
public TextView mTextViewLens;
public TextView mTextViewBrand;
public LensViewHolder(View itemView) {
super(itemView);
mImageLens = itemView.findViewById(R.id.lensimage);
mTextViewLens = itemView.findViewById(R.id.LensTextView);
mTextViewBrand = itemView.findViewById(R.id.LensBrandTextView);
}
}
//Constructor para ingresar la informacion del recycler al arraylist
public LensAdapter(ArrayList<LensItem> lensList){
mLensList = lensList;
}
//Crea el constructor para recibir la informacion del recyclerView
@Override
public LensViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lens_activity, parent, false);
LensViewHolder lvh = new LensViewHolder(v);
return lvh;
}
//recibe la informacion del recycleView y la muestra el los TextViews correspondientes
@Override
public void onBindViewHolder(LensViewHolder holder, int position) {
LensItem currentItem = mLensList.get(position);
holder.mImageLens.setImageResource(currentItem.getImageLens());
holder.mTextViewLens.setText(currentItem.getLens());
holder.mTextViewBrand.setText(currentItem.getBrandLens());
}
//Define cuantos items hay en la lista, por default es 0, pero con el cambio por .size
//es del tamaño del Array
@Override
public int getItemCount() {
return mLensList.size();
}
}
在这里我声明了get方法
package com.komorebiestudio.cam_report_funcionality;
public class LensItem {
private int mImageLens;
private String mLens;
private String mBrandLens;
public LensItem( int imageLens, String lens, String brandLens){
mImageLens = imageLens;
mLens = lens;
mBrandLens = brandLens;
}
public int getImageLens(){
return mImageLens;
}
public String getLens() {
return mLens;
}
public String getBrandLens() {
return mBrandLens;
}
}
这是将所有内容加在一起的活动
package com.komorebiestudio.cam_report_funcionality;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class LensActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lens_activity);
ArrayList <LensItem> lensItem = new ArrayList<>();
lensItem.add(new LensItem(R.drawable.ic_camera,"24mm","Zeiss Compact Prime"));
lensItem.add(new LensItem(R.drawable.ic_camera2,"35mm","Angenioux"));
lensItem.add(new LensItem(R.drawable.ic_camera3,"50mm","Cooke S5I"));
mRecyclerView = findViewById(R.id.lens_list);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new LensAdapter(lensItem);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
答案 0 :(得分:0)
我发现了错误,我在充气机上打错了布局。具体来说,我正在调用RecyclerView布局,应该调用CardView布局。
应该看起来像这样。
//Crea el constructor para recibir la informacion del recyclerView
@Override
public LensViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lens_list, parent, false);
LensViewHolder lvh = new LensViewHolder(v);
return lvh;
}