简体   繁体   English

Android-适配器NullPointerException

[英]Android - Adapter NullPointerException

I apologize in advance if this is a duplicate. 如果这是重复的事,我事先表示歉意。 I am still new to android development and tried looking for a resolution however could not find one that works. 我仍然是Android开发的新手,并尝试寻找一种解决方案,但是找不到可行的解决方案。

I am creating a to-do app and getting this error in my adapter. 我正在创建一个待办事项应用程序,并在适配器中收到此错误。

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)    

and the line of code that it is referencing to is 它所引用的代码行是

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

I believe that the position that I am setting it as is what is causing the error but I'm not sure how to fix it. 我相信我将其设置为错误的原因是什么导致该错误,但是我不确定如何解决。

Here's part of the rest of my code for reference: 这是我其余代码的一部分,以供参考:

MainActivity.Java - onActivityResult 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);
}

Second Activity - onSendActivity (Button) 第二活动-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();
}

Adapter 适配器

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;
    }
}

Model 模型

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;
    }
}    

Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

you should try to wrap it in an inner Holder class and define the parameters in that class 您应该尝试将其包装在内部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;

    }
}

change 更改

public class Adapter extends ArrayAdapter {

to

public class Adapter extends ArrayAdapter<Model> {

You have created model array of size 100 here: 您在此处创建了大小为100的模型数组:

modelItems = new Model[100];

So, 100 models are being created but all 100 indexes have null value. 因此,正在创建100个模型,但是所有100个索引的null

Then you have created ArrayList using that array: 然后,您使用该数组创建了ArrayList

ArrayList<Model> modelArrayList = new ArrayList<>(Arrays.asList(modelItems));

So again your modelArrayList has 100 null objects. 因此,您的modelArrayList具有100个null对象。 Which BTW you are using no where. 您在哪里使用哪个BTW。

You are passing modelItems into constructor of Adapter . 您正在将modelItems传递到Adapter构造函数中。 So now since all you items are null , you are getting this exception. 因此,由于所有项目均为null ,因此您将获得此异常。

Try to do something like this: 尝试做这样的事情:

ArrayList<Model> modelArrayList = new ArrayList<>();
modelArrayList.add(new Model(assignmentSentBack, (monthSentBack + 1) + "/" + daySentBack, 0));

Similarly add more model objects like that. 类似地,添加更多这样的模型对象。 Pass this modelArrayList in your adapter's constructor and use this (instead of array) to display the list. 在适配器的构造函数中传递此modelArrayList ,并使用它(而不是数组)显示列表。

lv = (ListView) findViewById(R.id.listAssignment);
ListAdapter adapter = new Adapter(this, modelArrayList)

And thus your adapter will be like this: 因此,您的适配器将如下所示:

ArrayList<Model> modelArrayList;
Context context;

public Adapter(Context context, ArrayList<Model> resource) {
    super(context, R.layout.row, resource);
    this.context = context;
    this.modelArrayList = resource;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM