回收器视图中的空对象引用
null object reference in recycler view
我创建了按目录显示图片的 recyclerView,
但是我在适配器中收到关于 getItemCount 的错误。错误说我的参考是空的。
我该如何解决这个问题?
其实我并没有想到要为此做些什么。
我的适配器(获取项目计数给我错误)
public class Adaptor extends RecyclerView.Adapter<Adaptor.ViewHolder> {
private Context context;
private List<model> list;
public Adaptor(Context context, List<model> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public Adaptor.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(context).inflate(R.layout.imageview,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adaptor.ViewHolder viewHolder, int i) {
model model=list.get(i);
Uri uri= Uri.parse(model.getPath());
File file=new File(String.valueOf(uri));
Bitmap bitmap= BitmapFactory.decodeFile(file.getAbsolutePath());
viewHolder.img.setImageBitmap(bitmap);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView img;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img=itemView.findViewById(R.id.img_view);
}
}
我的数据库
public class database extends SQLiteOpenHelper {
public database(@Nullable Context context) {
super(context, "add_image", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS ADD_IMAGE(_ID INTEGER PRIMARY KEY AUTOINCREMENT,_PICTURES varchar )");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long insert(String directory){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("_PICTURES",directory);
return db.insert("ADD_IMAGE",null,cv);
}
public Cursor cursor(){
SQLiteDatabase DB=this.getReadableDatabase();
return DB.rawQuery(" select * from ADD_IMAGE",null);
}
}
我的模型
public class model {
String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
我的activity
database database=new database(this);
SQLiteDatabase sqLiteDatabase;
RecyclerView rec_view;
List<model> list;
Context context;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Adaptor adaptor = new Adaptor(context, list);
rec_view = findViewById(R.id.rec_image);
rec_view.setAdapter(adaptor);
rec_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
btn = findViewById(R.id.loadimage);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
Cursor cursor = database.cursor();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
model model = new model();
model.setPath(cursor.getString(1));
list.add(model);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==RESULT_OK){
Uri tartgeturi=data.getData();
String directory=String.valueOf(tartgeturi);
database.insert(directory);
}
}
我希望我的错误得到修正
在你的activity,
而不是
List<model> list;
使用
List<model> list = new ArrayList(); // to initialize the list as empty
1) 首先,你必须在使用它之前初始化你的list
。(这就是你得到异常的原因)
2) 还有 set layout manager
到 recycler view
之前 set adapter
如下。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
// list initialization
list = new ArrayList();
Adaptor adaptor = new Adaptor(context, list);
rec_view = findViewById(R.id.rec_image);
// alwasy set layout manager before set adapter
rec_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
rec_view.setAdapter(adaptor);
btn = findViewById(R.id.loadimage);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
Cursor cursor = database.cursor();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
model model = new model();
model.setPath(cursor.getString(1));
list.add(model);
// notify your adapter that your data set is changed by adding folling line.
adapter.notifyDataSetChanged();
}
}
希望对你有帮助。
替换此
@Override
public int getItemCount() {
return list.size();
}
有了这个
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
我创建了按目录显示图片的 recyclerView, 但是我在适配器中收到关于 getItemCount 的错误。错误说我的参考是空的。
我该如何解决这个问题?
其实我并没有想到要为此做些什么。
我的适配器(获取项目计数给我错误)
public class Adaptor extends RecyclerView.Adapter<Adaptor.ViewHolder> {
private Context context;
private List<model> list;
public Adaptor(Context context, List<model> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public Adaptor.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(context).inflate(R.layout.imageview,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adaptor.ViewHolder viewHolder, int i) {
model model=list.get(i);
Uri uri= Uri.parse(model.getPath());
File file=new File(String.valueOf(uri));
Bitmap bitmap= BitmapFactory.decodeFile(file.getAbsolutePath());
viewHolder.img.setImageBitmap(bitmap);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView img;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img=itemView.findViewById(R.id.img_view);
}
}
我的数据库
public class database extends SQLiteOpenHelper {
public database(@Nullable Context context) {
super(context, "add_image", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS ADD_IMAGE(_ID INTEGER PRIMARY KEY AUTOINCREMENT,_PICTURES varchar )");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long insert(String directory){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("_PICTURES",directory);
return db.insert("ADD_IMAGE",null,cv);
}
public Cursor cursor(){
SQLiteDatabase DB=this.getReadableDatabase();
return DB.rawQuery(" select * from ADD_IMAGE",null);
}
}
我的模型
public class model {
String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
我的activity
database database=new database(this);
SQLiteDatabase sqLiteDatabase;
RecyclerView rec_view;
List<model> list;
Context context;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Adaptor adaptor = new Adaptor(context, list);
rec_view = findViewById(R.id.rec_image);
rec_view.setAdapter(adaptor);
rec_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
btn = findViewById(R.id.loadimage);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
Cursor cursor = database.cursor();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
model model = new model();
model.setPath(cursor.getString(1));
list.add(model);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==RESULT_OK){
Uri tartgeturi=data.getData();
String directory=String.valueOf(tartgeturi);
database.insert(directory);
}
}
我希望我的错误得到修正
在你的activity,
而不是
List<model> list;
使用
List<model> list = new ArrayList(); // to initialize the list as empty
1) 首先,你必须在使用它之前初始化你的list
。(这就是你得到异常的原因)
2) 还有 set layout manager
到 recycler view
之前 set adapter
如下。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
// list initialization
list = new ArrayList();
Adaptor adaptor = new Adaptor(context, list);
rec_view = findViewById(R.id.rec_image);
// alwasy set layout manager before set adapter
rec_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
rec_view.setAdapter(adaptor);
btn = findViewById(R.id.loadimage);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
Cursor cursor = database.cursor();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
model model = new model();
model.setPath(cursor.getString(1));
list.add(model);
// notify your adapter that your data set is changed by adding folling line.
adapter.notifyDataSetChanged();
}
}
希望对你有帮助。
替换此
@Override
public int getItemCount() {
return list.size();
}
有了这个
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}