NullPointer异常适配器getView

时间:2013-08-26 03:28:12

标签: android adapter

使用listView中的checkBox来生成上下文操作栏会给我以下nullpointer异常:

08-26 09:08:54.437: E/AndroidRuntime(32343): FATAL EXCEPTION: main
08-26 09:08:54.437: E/AndroidRuntime(32343): java.lang.NullPointerException
08-26 09:08:54.437: E/AndroidRuntime(32343):    at com.ia.developmentcheck.Allprojects$ArrayListAdapter$1.onCheckedChanged(Allprojects.java:332)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at android.widget.CompoundButton.setChecked(CompoundButton.java:124)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at android.widget.CompoundButton.toggle(CompoundButton.java:86)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at android.widget.CompoundButton.performClick(CompoundButton.java:98)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at android.view.View$PerformClick.run(View.java:9080)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at android.os.Handler.handleCallback(Handler.java:587)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at android.os.Looper.loop(Looper.java:130)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at android.app.ActivityThread.main(ActivityThread.java:3687)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at java.lang.reflect.Method.invoke(Method.java:507)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-26 09:08:54.437: E/AndroidRuntime(32343):    at dalvik.system.NativeStart.main(Native Method)

代码:

public class ArrayListAdapter扩展了BaseAdapter {

public Context mContext;
public LayoutInflater mInflater;
public ArrayList<HashMap<String,String>> mData;
private SparseBooleanArray mSelectedItemsIds;


public ArrayListAdapter(Context context, ArrayList<HashMap<String,String>> data){
    mSelectedItemsIds = new SparseBooleanArray();
    mData = data;
    this.mContext = context;
    mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public void toogleSelection(int position){
    selectView(position, !mSelectedItemsIds.get(position));
}

public void removeSelection(){
    mSelectedItemsIds = new SparseBooleanArray();
    notifyDataSetChanged();
}

public int getSelectedCount(){
    return mSelectedItemsIds.size();
}

public SparseBooleanArray getSelectedIds(){
    return mSelectedItemsIds;
}

private void selectView(int position, boolean value) {
    // TODO Auto-generated method stub
    if(value)
        mSelectedItemsIds.put(position, value);
    else
        mSelectedItemsIds.delete(position);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mData.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder vh;

    if(convertView == null){
        vh = new ViewHolder();
        convertView = mInflater.inflate(R.layout.projectlist_frame, null);
        vh.projectTitle = (TextView)convertView.findViewById(R.id.projecttitle);
        vh.projectSector = (TextView)convertView.findViewById(R.id.projectsector);
        vh.cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
        convertView.setTag(vh);

    } else{
        vh = (ViewHolder)convertView.getTag();
    }
    vh.projectTitle.setText(mData.get(position).get("title").toString());
    vh.projectSector.setText(mData.get(position).get("sector").toString());


    vh.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

            mSelectedItemsIds = list.getCheckedItemPositions();
            boolean hasCheckedElement = false;

            for(int i = 0; i < mSelectedItemsIds.size() && !hasCheckedElement; i++){
                hasCheckedElement = mSelectedItemsIds.valueAt(i);
            }if(hasCheckedElement){
                if(mMode == null){
                    startActionMode(mActionCallBack);
                    mMode.invalidate();
                }else {
                    mMode.invalidate();
                }
            } else {
                if(mMode !=null){
                    mMode.finish();
                }
            }
        }
    });

    return convertView;
}

第332行

for(int i = 0; i < mSelectedItemsIds.size() && !hasCheckedElement; i++)

这是在适配器的getView()

1 个答案:

答案 0 :(得分:3)

问题似乎是您的列表未启用多个选择(或显然是选择)。来自the docs for getCheckedItemPositions()

  

返回:SparseBooleanArray,每次调用get(int position)时返回true,其中position是列表中的位置,如果选择模式设置为CHOICE_MODE_NONE,则返回null

也许您打算拨打getCheckedItemPosition()(没有“s”)?确保列表选择模式设置为适当的值。