0

抱歉,如果这看起来很愚蠢,我就是无法理解我遇到的这个运行时错误。作为参考,这是我的代码:

MainActivity.java

MainListViewRecordAdapter.java

目前,我在 MainListViewRecordAdapter 的第 27 行收到 NullPointerException。当我运行调试器时,int位置一直为0,这导致代码没有将一个项目从ArrayList中分级出来,从而导致NullPointerException。唯一的问题是,我不明白为什么会这样,为什么 position 的值总是 0?

4

1 回答 1

2

您还没有初始化 Arraylist 项目并删除行位置 = 1;。执行以下操作:

package com.chigstuff.fun;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MainListViewRecordAdapter extends ArrayAdapter<MainListViewRecord> {
    private ArrayList<MainListViewRecord> items;

    public MainListViewRecordAdapter(Context context, int textViewResourceId, ArrayList<MainListViewRecord> items) {
        super(context, textViewResourceId, items);
this.items=items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.main_list_view_record, null);
        }
        MainListViewRecord item = items.get(position);
        if (item != null) {
            TextView main_list_view_text = (TextView) v.findViewById(R.id.main_list_view_text);

            if (main_list_view_text != null) {
                main_list_view_text.setText("What text");
            }
        }

        return v;
    }
}
于 2012-07-13T04:48:13.293 回答