为什么我在listAdapter上获得空值?

时间:2019-07-19 12:32:41

标签: android listadapter

我的列表适配器listAdapter = new ListAdapter(models, this);出现错误,我为自己指定了自定义适配器名称ListAdapter

我认为因为没有数据,所以我使用model.add放入了数据,并且尝试以编程方式放入数据后,仍然出现错误,说明了同样的事情。

我该如何解决?下面我给你一些代码示例。

MainActivity:

private ListAdapter listAdapter;
private ArrayList<Model> models;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_income);
    models = new ArrayList<>();
    listAdapter = new ListAdapter(models, this);
    listAdapter.notifyDataSetChanged();
    listView.setAdapter(listAdapter);
    findView();
    initView();
    initListener();
}

@Override
public void findView() {
    listView = findViewById(R.id.list_pemasukan);
    dateSet = findViewById(R.id.date_set);
    bAddIncome = findViewById(R.id.add_income);
    frameLayout = findViewById(R.id.frame_layout_income);
    bPickDate = findViewById(R.id.pick_date);
}

它说我的适配器为null,我不知道它在哪里设置此值。我尝试使用model.add在模型上放置一些数据,但仍然没有改变。

ListAdapter:

public class ListAdapter extends ArrayAdapter<Model> {

    private ArrayList<Model> dataTable;
    Context context;

    private static class ViewHolder {
        TextView txInOut;
        TextView txDay;
        TextView txMonth;
        TextView txYear;
        TextView Descriptions;
    }

    public ListAdapter(ArrayList<Model> data, Context context) {
        super(context, R.layout.list_row, data);
        this.context = context;
        this.dataTable = data;
    }

    private int lastPosition = -1;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Model dataModel = getItem(position);
        ViewHolder viewHolder;
        final View result;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.list_row, parent, false);
            viewHolder.txInOut = convertView.findViewById(R.id.pemasukan);
            viewHolder.txDay = convertView.findViewById(R.id.tanggal);
            viewHolder.txMonth = convertView.findViewById(R.id.bulan);
            viewHolder.txYear = convertView.findViewById(R.id.year);
            viewHolder.Descriptions = convertView.findViewById(R.id.keterangan);

            result = convertView;

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            result = convertView;
        }

        Animation animation = AnimationUtils.loadAnimation(context, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
        result.startAnimation(animation);
        lastPosition = position;

        viewHolder.txInOut.setText(dataModel.getPemasukan());
        viewHolder.txDay.setText(dataModel.getDay());
        viewHolder.txMonth.setText(dataModel.getMonth());
        viewHolder.txYear.setText(dataModel.getYear());
        viewHolder.Descriptions.setText(dataModel.getKeterangan());

        return convertView;
    }
}

这是我的错误,我不知道我的错误来自哪里。

错误消息:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.educastudio.marbelmoneymanager.IncomeActivity.onCreate(IncomeActivity.java:53)
    at android.app.Activity.performCreate(Activity.java:6267)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2479) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5420) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-19 19:42:11.854 6740-6740/com.educastudio.marbelmoneymanager I/Process: Sending signal. PID: 6740 SIG: 9

5 个答案:

答案 0 :(得分:2)

使用listview之前,应先对其进行初始化。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_income);
    findView();
    initView();
    initListener();
    models = new ArrayList<>();
    listAdapter = new ListAdapter(models, this);
    listAdapter.notifyDataSetChanged();
    listView.setAdapter(listAdapter);
}

答案 1 :(得分:0)

您的listView尚未初始化。

findView行上方添加listView.setAdapter(listAdapter);

有关NullPointException及其发生原因的更多信息,click here

答案 2 :(得分:0)

您不需要第一次致电notifyDataSetChanged。另外,您还需要通过findViewById初始化视图,然后才能访问它们:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_income);
    findView();
    initView();
    initListener();

    models = new ArrayList<>();
    listAdapter = new ListAdapter(models, this);
    listView.setAdapter(listAdapter);

}

答案 3 :(得分:0)

您只需要在将适配器分配给列表视图之前初始化视图,否则您的代码伴侣就没有问题。

在onCreateMethod中-

findView();
initView();
initListener();
models = new ArrayList<>();
listAdapter = new ListAdapter(models, this);
listAdapter.notifyDataSetChanged();
listView.setAdapter(listAdapter);

答案 4 :(得分:-2)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_income);
     findView();
    initView();
    initListener();
    models = new ArrayList<>();
    listAdapter = new ListAdapter(models, this);
    listAdapter.notifyDataSetChanged();
    listView.setAdapter(listAdapter);

}