很抱歉,如果这看起来很愚蠢,我就无法理解我遇到的这个运行时错误。作为参考,这是我的代码:
MainListViewRecordAdapter.java
目前,我在MainListViewRecordAdapter的第27行遇到NullPointerException。当我运行调试器时,int位置始终为0,这导致代码不会将项目从ArrayList中删除,从而导致NullPointerException。唯一的问题是,我不明白为什么会发生这种情况,为什么位置始终具有值0?
答案 0 :(得分: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;
}
}