调用自定义列表视图管理器时出现nullPointerException

时间:2017-07-20 14:58:31

标签: java android nullpointerexception

我想使用自定义列表view.first,我创建了一个名为Main List的java类,其中包含两个变量(name,image)。 这个类代码:

public class MainList {
    private String name;
    private String image;

    public MainList(String name, String image) {
        this.setName(name);
        this.setImage(image);
    }
public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

然后我创建ListManager类扩展BaseAdapter,这个类代码:

public class ListManager extends BaseAdapter {
    private Context context;
    private ArrayList<MainList> list;
    private LayoutInflater inflater;

    public ListManager(Context context, ArrayList<MainList> list) {
        this.setContext(context);
        this.setList(list);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        try {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewHolder viewHolder = new viewHolder();

            if(view == null){
                inflater.inflate(R.layout.list,null);
                viewHolder.name = (TextView) view.findViewById(R.id.listTitle);
                viewHolder.image = (ImageView) view.findViewById(R.id.listImage);
            }else viewHolder = (viewHolder) view.getTag();

            if (list.size() > 0){
                MainList mainList = list.get(i);
                viewHolder.name.setText(mainList.getName());
            }
        }catch (Exception e){
            Log.i("Tag:LM", "getView: " + e.toString());
        }


        return view;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public ArrayList<MainList> getList() {
        return list;
    }

    public void setList(ArrayList<MainList> list) {
        this.list = list;
    }
    private static class viewHolder{
        private TextView name;
        private ImageView image;
    }
}

*我只是设置TextView来测试我的代码。创建此类后,在list活动中使用此类代码:

public class ListActivity extends AppCompatActivity {

    private ListView listView;
    private ListManager adapter;
    private ArrayList<MainList> arrayList;
    private static final String TAG = "Tag:ListActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        try {
            listView = (ListView) findViewById(R.id.list);

            setData();

            adapter = new ListManager(this, arrayList);

            listView.setAdapter(adapter);
        }catch (Exception e){
            Log.i(TAG, "onCreate: "+e.toString());
        }
    }
    private void setData(){
        MainList mainList = new MainList("all","khddai");
        MainList mainList1 = new MainList("sf","khddai");
        MainList mainList2 = new MainList("adfdli","khdfai");
        MainList mainList3 = new MainList("aldi","khdfai");

        arrayList.add(mainList);
        arrayList.add(mainList1);
        arrayList.add(mainList2);
        arrayList.add(mainList3);
    }
}

android log cat: 07-20 14:53:15.240 9861-9861 / com.package.app I / Tag:ListActivity:onCreate:java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

在ListActivity private ArrayList<MainList> arrayList;中,您没有创建对象。没有使用setData()方法中使用该变量的对象。

在类变量中创建对象,

private ArrayList<MainList> arrayList = new ArrayList<>();

之前将其添加到setData中的列表中,如此

private void setData(){
        arrayList = new ArrayList<>();
        MainList mainList = new MainList("all","khddai");
        MainList mainList1 = new MainList("sf","khddai");
        MainList mainList2 = new MainList("adfdli","khdfai");
        MainList mainList3 = new MainList("aldi","khdfai");

        arrayList.add(mainList);
        arrayList.add(mainList1);
        arrayList.add(mainList2);
        arrayList.add(mainList3);
    }

和SRB禁止提及的另一个问题是,您应该在获得孩子之前创建视图,在从布局充气机view = inflater.inflate(R.layout.list,null);

获取后,您错过了获取视图
public View getView(int i, View view, ViewGroup viewGroup) {
        try {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewHolder viewHolder = new viewHolder();

            if(view == null){
                view = inflater.inflate(R.layout.list,null);
                viewHolder.name = (TextView) view.findViewById(R.id.listTitle);
                viewHolder.image = (ImageView) view.findViewById(R.id.listImage);
                view.setTag(viewHolder);
            }else viewHolder = (viewHolder) view.getTag();

            if (list.size() > 0){
                MainList mainList = list.get(i);
                viewHolder.name.setText(mainList.getName());
            }
        }catch (Exception e){
            Log.i("Tag:LM", "getView: " + e.toString());
        }


        return view;
    }