自定义列表视图与android中的复选框

时间:2013-11-29 12:49:36

标签: java android android-listview nullpointerexception android-custom-view

我正在尝试在自定义列表视图中添加复选框,但它给出了NullPointerException错误。

这是我的MainActivity类

public class Classes extends Activity {

ImageView imageViewNewClass;
ListView mListView;
String[] stg1;
List<String[]> names2 = null;
DataManipulatorClass dataManipulator;
CustomAdapter customAdapter;
public Classes classes = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.classes);

    imageViewNewClass = (ImageView) findViewById(R.id.newclass);
    mListView = (ListView) findViewById(R.id.displaydata);

    Resources res =getResources();
    classes = this;
    customAdapter=new CustomAdapter( classes, stg1,res );
    mListView.setAdapter( customAdapter );

    imageViewNewClass.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(Classes.this, Class_Create.class);
            startActivity(intent);
        }
    });

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View item,
                int position, long id) {
            Toast.makeText(getApplicationContext(),
                    "Listview item clicked", Toast.LENGTH_LONG).show();
        }
    });

    dataManipulator = new DataManipulatorClass(this);
    names2 = dataManipulator.selectAll();

    stg1 = new String[names2.size()];
    int x = 0;
    String stg;

    for (String[] name : names2) {
        stg = "Class Name : " + name[1];
        stg1[x] = stg;
        x++;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, stg1);

    mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.check,
            stg1));
    mListView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}
}

这是我的CustomAdapter类

public class CustomAdapter extends BaseAdapter {

/*********** Declare Used Variables *********/
private Activity activity;
private String[] data;
private static LayoutInflater inflater = null;
public Resources res;
int i = 0;

/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, String[] stg1, Resources resLocal) {

    /********** Take passed values **********/
    activity = a;
    data = stg1;
    res = resLocal;

    /*********** Layout inflator to call external xml layout () ***********/
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
    if (data.length <= 0)
        return 1;
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder {
    public CheckBox checkBox;
}

public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ViewHolder holder;

    if (convertView == null) {
        vi = inflater.inflate(R.layout.check, null);
        holder = new ViewHolder();
        holder.checkBox = (CheckBox) vi.findViewById(R.id.checkBox1);
        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();
    return vi;
}
public void onClick(View v) {
    Log.v("CustomAdapter", "=====Row button clicked=====");
}
}

除此之外,我还有2个活动,一个用于创建数据库并在其中存储值,另一个用于传递数据。

我想要做的是当用户按下Classes类中的按钮新活动打开并要求输入数据时,我将该数据存储在数据库中,然后尝试在类类中检索该数据。所以我采取了列表视图显示数据。

现在使用listview我还想显示复选框,当用户选中任何复选框时,应打开一个新活动并显示该复选框的相关内容。

但是当我尝试使用列表框实现复选框时,我收到错误。

这是我的原木猫http://i.share.pho.to/e06cc7b5_o.png

提前谢谢你......

3 个答案:

答案 0 :(得分:1)

您已使用名为“stg1”的字符串数组初始化自定义适配器,但是当您在活动中将此字符串用于此行时,“stg1”本身尚未初始化。

customAdapter=new CustomAdapter( classes, stg1,res );

然后,您已在此行

稍后初始化了stg1数组
stg1 = new String[names2.size()];

因此,当您第一次在适配器中使用此null对象时,您的适配器无法确定GetCount方法中的数组大小。这就是你得到空指针异常的原因。

答案 1 :(得分:0)

您可能忘记在适配器的构造函数中初始化data数组

在Adapter的构造函数

中尝试这样
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, String[] stg1, Resources resLocal) {

/********** Take passed values **********/
activity = a;
data = new String[stg1.length]; //// Add this line
data = stg1;
res = resLocal;
.
.
.




}

答案 2 :(得分:0)

传递给CustomAdapter的字符串数组为null

customAdapter=new CustomAdapter( classes, stg1,res ); // here stg1 is null