提问人:Programming Solutions 提问时间:11/18/2021 最后编辑:BrunoProgramming Solutions 更新时间:11/18/2021 访问量:426
RecyclerView 适配器变为 null
RecyclerView Adapter becomes null
问:
我在我的应用程序中嵌套了 RecyclerViews,我从中填充问题及其各自的答案。问题是当我尝试附加图像时,Child RecyclerView 适配器以某种方式获得 null。我尝试解决它,但没有成功。我希望有人能帮忙。
我的第一个适配器:
public class QuestionsAdapter extends
RecyclerView.Adapter<QuestionsAdapter.QuestionsViewHolder>
implements Parcelable {
ArrayList<QuestionsData> questionsDataArrayList;
Context context;
OptionsAdapter optionsAdapter;
Fragment fragment;
public QuestionsAdapter(Context context,ArrayList<QuestionsData>questionsDataArrayList,Fragment fragment) {
this.questionsDataArrayList = questionsDataArrayList;
this.context = context;
this.fragment = fragment;
}
protected QuestionsAdapter(Parcel in) {
}
public static final Creator<QuestionsAdapter> CREATOR = new Creator<QuestionsAdapter>() {
@Override
public QuestionsAdapter createFromParcel(Parcel in) {
return new QuestionsAdapter(in);
}
@Override
public QuestionsAdapter[] newArray(int size) {
return new QuestionsAdapter[size];
}
};
@NonNull
@Override
public QuestionsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_questions_item, parent, false);
return new QuestionsViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull QuestionsViewHolder holder, int position) {
holder.question_title.setText(questionsDataArrayList.get(position).getQuesiton());
optionsAdapter = new OptionsAdapter(context, questionsDataArrayList.get(position).getQuestionOptionsArrayList()
, questionsDataArrayList.get(position).getId(), (QuestionsFragment) fragment);
holder.questions_option_recyclerView.setLayoutManager(new GridLayoutManager(context, 1));
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 1) {
@Override
public boolean canScrollVertically() {
return false;
}
};
holder.questions_option_recyclerView.setLayoutManager(gridLayoutManager);
holder.questions_option_recyclerView.setItemViewCacheSize(questionsDataArrayList.size());
holder.questions_option_recyclerView.setAdapter(optionsAdapter);
}
@Override
public int getItemCount() {
return questionsDataArrayList.size();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
}
class QuestionsViewHolder extends RecyclerView.ViewHolder {
private final TextView question_title;
private final RecyclerView questions_option_recyclerView;
QuestionsViewHolder(@NonNull View itemView) {
super(itemView);
question_title = itemView.findViewById(R.id.question_title);
questions_option_recyclerView = itemView.findViewById(R.id.questions_option_recyclerView);
}
}
public void setImage(ArrayList<ImagePathModel> imagesList) {
ArrayList<Uri> images = new ArrayList<>();
for (ImagePathModel image : imagesList
) {
images.add(image.getUri());
}
optionsAdapter.updateImages(images);
}
}
第二个适配器:
public class OptionsAdapter extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<QuestionOptions> questionOptionsArrayList;
Context context;
String currentQuestionID;
public ImagesRecyclerViewAdapter imagesRecyclerViewAdapter;
QuestionAnswerModel.Answers answers;
QuestionsFragment fragment;
SharedPreferenceHandler sharedPreferenceHandler;
public OptionsAdapter(Context context, ArrayList<QuestionOptions> questionOptionsArrayList, String currentQuestionID, QuestionsFragment fragment) {
this.questionOptionsArrayList = questionOptionsArrayList;
this.context = context;
this.currentQuestionID = currentQuestionID;
this.fragment = fragment;
answers = new QuestionAnswerModel.Answers();
sharedPreferenceHandler = new SharedPreferenceHandler(context);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if (viewType == 1) { // for call layout
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_answer_options, parent, false);
return new OptionsViewHolder(view);
} else if (viewType == 2) { // for email layout
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_text_item, parent, false);
return new TextViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_images_item, parent, false);
return new ImagesViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if ((questionOptionsArrayList.get(position).getInputType().equalsIgnoreCase("image"))) {
imagesRecyclerViewAdapter = new ImagesRecyclerViewAdapter(context, answers.getImages());
((ImagesViewHolder) holder).recyclerView_images.setAdapter(imagesRecyclerViewAdapter);
} else if (questionOptionsArrayList.get(position).getInputType().equalsIgnoreCase("radio")) {
if (questionOptionsArrayList.get(position).getValue().equalsIgnoreCase("1")) {
questionOptionsArrayList.get(position).setIsRadioButtonSelected(true);
}
((OptionsViewHolder) holder).answer_option.setText(questionOptionsArrayList.get(position).getOption());
((OptionsViewHolder) holder).answer_option.setChecked(questionOptionsArrayList.get(position).getIsRadioButtonSelected());
} else if ((questionOptionsArrayList.get(position).getInputType().equalsIgnoreCase("text"))) {
((TextViewHolder) holder).option_title.setText(questionOptionsArrayList.get(position).getOption());
((TextViewHolder) holder).et_answer.setHint("Enter Value");
if (!questionOptionsArrayList.get(position).getValue().isEmpty()) {
((TextViewHolder) holder).et_answer.setText(questionOptionsArrayList.get(position).getValue());
}
}
}
@Override
public int getItemCount() {
return questionOptionsArrayList.size();
}
@Override
public int getItemViewType(int position) {
if (questionOptionsArrayList.get(position).getInputType().equalsIgnoreCase("radio")) {
return 1;
} else if (questionOptionsArrayList.get(position).getInputType().equalsIgnoreCase("text")) {
return 2;
} else {
return 3;
}
}
class OptionsViewHolder extends RecyclerView.ViewHolder {
CheckBox answer_option;
OptionsViewHolder(@NonNull View itemView) {
super(itemView);
answer_option = itemView.findViewById(R.id.answer_option);
answer_option.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
questionOptionsArrayList.get(getAdapterPosition()).setIsRadioButtonSelected(answer_option.isChecked());
answers.setQuestion_id(currentQuestionID);
answers.getType().add(questionOptionsArrayList.get(getAdapterPosition()).getInputType());
answers.getOption_id().add(questionOptionsArrayList.get(getAdapterPosition()).getId());
answers.getValue().add("1");
searchDeleteAndAddItem(currentQuestionID,
questionOptionsArrayList.get(getAdapterPosition()).getId(),
questionOptionsArrayList.get(getAdapterPosition()).getInputType(),
"1");
// searchAndAddItem(answers);
} else {
questionOptionsArrayList.get(getAdapterPosition()).setIsRadioButtonSelected(answer_option.isChecked());
answers.setQuestion_id(currentQuestionID);
answers.getType().add(questionOptionsArrayList.get(getAdapterPosition()).getInputType());
answers.getOption_id().add(questionOptionsArrayList.get(getAdapterPosition()).getId());
answers.getValue().add("0");
searchDeleteAndAddItem(currentQuestionID,
questionOptionsArrayList.get(getAdapterPosition()).getId(),
questionOptionsArrayList.get(getAdapterPosition()).getInputType(),
"0");
}
}
});
}
}
class TextViewHolder extends RecyclerView.ViewHolder {
private final EditText et_answer;
private final TextView option_title;
TextViewHolder(@NonNull View itemView) {
super(itemView);
et_answer = itemView.findViewById(R.id.et_answer);
option_title = itemView.findViewById(R.id.option_title);
et_answer.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
answers.setQuestion_id(currentQuestionID);
answers.getType().add(questionOptionsArrayList.get(getAdapterPosition()).getInputType());
answers.getOption_id().add(questionOptionsArrayList.get(getAdapterPosition()).getId());
answers.getValue().add(et_answer.getText().toString());
searchDeleteAndAddItem(currentQuestionID, questionOptionsArrayList.get(getAdapterPosition()).getId(), questionOptionsArrayList.get(getAdapterPosition()).getInputType(),
s.toString());
} else {
removeItem(questionOptionsArrayList.get(getAdapterPosition()).getId());
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
class ImagesViewHolder extends RecyclerView.ViewHolder {
private final RecyclerView recyclerView_images;
private final MaterialButton btn_add_images;
ImagesViewHolder(@NonNull View itemView) {
super(itemView);
btn_add_images = itemView.findViewById(R.id.btn_add_images);
recyclerView_images = itemView.findViewById(R.id.recyclerView_images);
recyclerView_images.hasFixedSize();
recyclerView_images.setLayoutManager(new GridLayoutManager(context, 3));
btn_add_images.setOnClickListener(v -> {
sharedPreferenceHandler.setString("questionID", currentQuestionID);
sharedPreferenceHandler.setString("inputType", questionOptionsArrayList.get(getAdapterPosition()).getInputType());
sharedPreferenceHandler.setString("ID", questionOptionsArrayList.get(getAdapterPosition()).getId());
Intent intent = new Intent(context, EditImageActivity.class);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
fragment.startActivityForResult(intent, 300);
}
);
}
}
public void searchAndAddItem(QuestionAnswerModel.Answers answer) {
Constants.CURRENT_INSPECTION.answersArrayList.add(answer);
}
public void searchDeleteAndAddItem(String questionID, String optionID, String inputType, String value) {
boolean newQuestion = false;
if (Constants.CURRENT_INSPECTION.answersArrayList.size() == 0) {
Constants.CURRENT_INSPECTION.answersArrayList.add(answers);
} else
for (int index = 0; index < Constants.CURRENT_INSPECTION.answersArrayList.size(); index++) {
if (Constants.CURRENT_INSPECTION.answersArrayList.get(index).getQuestion_id().equalsIgnoreCase(questionID)) {
for (int i = 0; i < Constants.CURRENT_INSPECTION.answersArrayList.get(index).getOption_id().size(); i++) {
if (Constants.CURRENT_INSPECTION.answersArrayList.get(index).getOption_id().get(i).equalsIgnoreCase(optionID)) {
Constants.CURRENT_INSPECTION.answersArrayList.get(index).getOption_id().remove(i);
Constants.CURRENT_INSPECTION.answersArrayList.get(index).getValue().remove(i);
Constants.CURRENT_INSPECTION.answersArrayList.get(index).getType().remove(i);
break;
}
}
Constants.CURRENT_INSPECTION.answersArrayList.get(index).getOption_id().add(optionID);
Constants.CURRENT_INSPECTION.answersArrayList.get(index).getType().add(inputType);
Constants.CURRENT_INSPECTION.answersArrayList.get(index).getValue().add(value);
} else {
newQuestion = true;
}
}
if (newQuestion) {
Constants.CURRENT_INSPECTION.answersArrayList.add(answers);
}
}
public void removeItem(String optionID) {
for (int index = 0; index < answers.getOption_id().size(); index++) {
if (answers.getOption_id().get(index).equalsIgnoreCase(optionID)) {
answers.getOption_id().remove(index);
answers.getType().remove(index);
answers.getValue().remove(index);
}
}
}
public void updateImages(ArrayList<Uri> uris) {
String questionID = sharedPreferenceHandler.getString("questionID");
String inputType = sharedPreferenceHandler.getString("inputType");
String ID = sharedPreferenceHandler.getString("ID");
imagesRecyclerViewAdapter.notifyChange(uris);
}
}
第三个适配器:
public class ImagesRecyclerViewAdapter extends RecyclerView.Adapter<ImagesRecyclerViewAdapter.ViewHolder> {
ArrayList<Uri> images;
Context context;
public ImagesRecyclerViewAdapter(Context context, ArrayList<Uri> images) {
this.images = images;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_images_recyclerview_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
try {
holder.images_item.setImageBitmap(MediaStore.Images.Media.getBitmap(context.getContentResolver(), images.get(position)));
} catch (IOException e) {
e.printStackTrace();
}
}
public void notifyChange(ArrayList<Uri> images) {
images.addAll(images);
this.notifyDataSetChanged();
}
@Override
public int getItemCount() {
return images.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView images_item, iv_close_image;
public ViewHolder(@NonNull View itemView) {
super(itemView);
images_item = itemView.findViewById(R.id.images_item);
iv_close_image = itemView.findViewById(R.id.iv_close_image);
iv_close_image.setOnClickListener(view -> {
images.remove(getAdapterPosition());
notifyDataSetChanged();
});
images_item.setOnClickListener(view -> {
Intent viewIntent = new Intent(context, ImageViewerActivity.class);
viewIntent.putExtra("uri", images.get(getAdapterPosition()).toString());
context.startActivity(viewIntent);
});
}
}
}
日志:: 我正面临这个问题。
2021-11-18 16:09:33.641 20945-20945/com.arcore.properscan E/AndroidRuntime:致命异常:main 进程:com.arcore.properscan,PID:20945 java.lang.RuntimeException:传递结果 ResultInfo{who=null, request=490889150, result=-1, data=Intent { (具有 extras) }} 到活动 {com.arcore.properscan/com.arcore.properscan.Activities.InspectionSubCategoryActivity}: java.lang.NullPointerException:尝试调用虚拟方法“void” com.arcore.properscan.Adapters.ImagesRecyclerViewAdapter.notifyChange(java.util.ArrayList)' 在 null 对象引用上 在 android.app.ActivityThread.deliverResults(ActivityThread.java:4626) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:4670) 在 android.app.ActivityThread.-wrap19(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1829) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:198) 在 android.app.ActivityThread.main(ActivityThread.java:7055) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:523) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836) 原因:java.lang.NullPointerException:尝试调用虚拟方法“void” com.arcore.properscan.Adapters.ImagesRecyclerViewAdapter.notifyChange(java.util.ArrayList)' 在 null 对象引用上 在 com.arcore.properscan.Adapters.OptionsAdapter.updateImages(OptionsAdapter.java:295) 在com.arcore.properscan.Adapters.QuestionsAdapter.setImage(QuestionsAdapter.java:114) 在 com.arcore.properscan.Fragments.QuestionsFragment.onActivityResult(QuestionsFragment.java:116) 在 androidx.fragment.app.FragmentManager$9.onActivityResult(FragmentManager.java:2905) 在 androidx.fragment.app.FragmentManager$9.onActivityResult(FragmentManager.java:2885) 在 androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:392) 在 androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:351) 在 androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:647) 在 androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:164) 在 android.app.Activity.dispatchActivityResult(Activity.java:7634) 在 android.app.ActivityThread.deliverResults(ActivityThread.java:4622) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:4670) 在 android.app.ActivityThread.-wrap19(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1829) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:198) 在 android.app.ActivityThread.main(ActivityThread.java:7055) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:523) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836)
答: 暂无答案
评论
onActivityResult()
First Adapter
Parcelable
Adapter
Activity