【问题标题】:Custom list view with check box in androidandroid中带有复选框的自定义列表视图
【发布时间】:2013-11-29 12:49:36
【问题描述】:

我正在尝试在自定义列表视图中添加复选框,但它给出了 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 类中的按钮新活动打开并要求输入数据时,我将该数据存储在数据库中,然后尝试在 classes 类中检索该数据。所以我采取了显示数据的列表视图。

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

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

这是我的日志猫http://i.share.pho.to/e06cc7b5_o.png

提前谢谢你...

【问题讨论】:

  • 您似乎忘记在使用之前初始化适配器

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


【解决方案1】:

您已经使用名为“stg1”的字符串数组初始化了您的自定义适配器,但是当您在 Activity 中将它用于此行时,“stg1”本身并没有被初始化。

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

那么你已经通过这一行在稍后的地方初始化了你的 stg1 数组

stg1 = new String[names2.size()];

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

【讨论】:

    【解决方案2】:

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

    在适配器的构造函数中尝试这样

    /************* 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;
    .
    .
    .
    
    
    
    
    }
    

    【讨论】:

      【解决方案3】:

      您传递给 CustomAdapter 的字符串数组为空

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-23
        • 1970-01-01
        • 2015-03-01
        相关资源
        最近更新 更多