gpt4 book ai didi

java - Android - 适配器 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 03:43:20 29 4
gpt4 key购买 nike

如果这是重复的,我提前道歉。我对 Android 开发还是新手,并尝试寻找解决方案,但找不到有效的解决方案。

我正在创建一个待办事项应用程序并在我的适配器中收到此错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String assignment.Model.getAssignment()' on a null object reference
at assignment.Adapter.getView(Adapter.java:39)

它引用的代码行是

assignment.setText(modelItems[position].getAssignment());

我相信我设置的位置是导致错误的原因,但我不确定如何修复它。

这是我的其余代码的一部分供引用:

MainActivity.Java - onActivityResult

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

String assignmentSentBack = data.getStringExtra("editAssignment");
Integer monthSentBack = data.getIntExtra("month", 0);
Integer daySentBack = data.getIntExtra("day", 0);

modelItems = new Model[100];
ArrayList<Model> modelArrayList = new ArrayList<>(Arrays.asList(modelItems));
modelArrayList.add(new Model(assignmentSentBack, (monthSentBack + 1) + "/" + daySentBack, 0));
lv = (ListView) findViewById(R.id.listAssignment);
ListAdapter adapter = new Adapter(this, modelItems);
lv.setAdapter(adapter);
}

第二个 Activity - onSendActivity(按钮)

public void onSendAssignment (View view) {

EditText editAssignmentET = (EditText)
findViewById(R.id.editAssignment);

String editAssignment = String.valueOf(editAssignmentET.getText());

DatePicker datePickerDP = (DatePicker)
findViewById (R.id.datePicker);

Integer month = Integer.valueOf(datePickerDP.getMonth());

Integer day = Integer.valueOf(datePickerDP.getDayOfMonth());

Intent goingBack = new Intent();

goingBack.putExtra("editAssignment", editAssignment);
goingBack.putExtra ("month", month);
goingBack.putExtra("day", day);

setResult(RESULT_OK, goingBack);

finish();
}

适配器

public class Adapter extends ArrayAdapter {

Model[] modelItems = null;
Context context;

public Adapter(Context context, Model[] resource) {
super(context, R.layout.row, resource);
this.context = context;
this.modelItems = resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, parent, false);
TextView assignment = (TextView) convertView.findViewById(R.id.assignment);
TextView dueDate = (TextView) convertView.findViewById(R.id.dueDate);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox);
assignment.setText(modelItems[position].getAssignment());
dueDate.setText(modelItems[position].getDueDate());
if (modelItems[position].getValue() == 1)
cb.setChecked(true);
else
cb.setChecked(false);

return convertView;
}
}

型号

public class Model {

String assignment;
String dueDate;
int value;

Model (String assignment, String dueDate, int value){
this.assignment = assignment;
this.dueDate = dueDate;
this.value = value;
}

public String getAssignment(){
return this.assignment;
}

public String getDueDate(){
return this.dueDate;
}

public int getValue(){
return this.value;
}
}

感谢任何帮助。谢谢。

最佳答案

您应该尝试将其包装在内部 Holder 类中并在该类中定义参数

public class Adapter extends ArrayAdapter {

Model[] modelItems = null;
Context context;

public Adapter(Context context, Model[] resource) {
super(context, R.layout.row, resource);
this.context = context;
this.modelItems = resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
holder = new ViewHolder();

convertView = vi.inflate(R.layout.list_layout, null);

// Find the child views.
holder.assignment= (TextView) convertView.findViewById(R.id.txt_name);
holder.dueDate= (Button) convertView.findViewById(R.id.btn_invite);
holder.cb= (Button) convertView.findViewById(R.id.btn_track);
convertView.setTag(holder);

//....

}
// Reuse existing row view
else {
holder = (ViewHolder)convertView.getTag();

}

return convertView;
}
class ViewHolder {

TextView assignment;
TextView dueDate;
CheckBox cb;

}
}

关于java - Android - 适配器 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36588870/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com