Android NullPointer异常适配器getView

Android NullPointer异常适配器getView,android,adapter,Android,Adapter,使用listView中的复选框生成上下文操作栏会导致以下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.develo

使用listView中的复选框生成上下文操作栏会导致以下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)
守则:

公共类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;
}
公共上下文mContext;
公共布局更扁平,更扁平;
公共阵列列表mData;
专用SparseBooleanArray MSelectEditEMSID;
公共ArrayListAdapter(上下文上下文,ArrayList数据){
mSelectedItemsIds=new SparseBooleanArray();
mData=数据;
this.mContext=上下文;
mInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
}
公共选择(内部位置){
选择查看(位置,!mSelectedItemsIds.get(位置));
}
公共选举{
mSelectedItemsIds=new SparseBooleanArray();
notifyDataSetChanged();
}
public int getSelectedCount(){
返回mSelectedItemsIds.size();
}
公共SparseBooleanArray getSelectedIds(){
返回mSelectedItemsIds;
}
私有void selectView(int位置,布尔值){
//TODO自动生成的方法存根
如果(值)
mSelectedItemsIds.put(位置、值);
其他的
mSelectedItemsIds.delete(位置);
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回mData.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共长getItemId(int arg0){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视窗座;
if(convertView==null){
vh=新的视窗支架();
convertView=mInflater.充气(R.layout.projectlist\u框架,空);
vh.projectTitle=(TextView)convertView.findViewById(R.id.projectTitle);
vh.projectSector=(TextView)convertView.findViewById(R.id.projectSector);
vh.cb=(复选框)convertView.findViewById(R.id.checkBox1);
convertView.setTag(vh);
}否则{
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(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
//TODO自动生成的方法存根
mSelectedItemsIds=list.getCheckedItemPositions();
布尔hasCheckedElement=false;
对于(int i=0;i
第332行

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

这是在适配器的
getView()
上出现的问题似乎是您的列表没有为多个选择启用(或者根本没有为选择启用)。来自:

返回:SparseBooleanArray,对于每次调用get(int position)都返回true,其中position是列表中的一个位置,或者
null
如果选择模式设置为choice\u mode\u NONE


可能您想调用
GetCheckEditePosition()
(不带“s”)?请确保列表选择模式设置为适当的值。

问题似乎是您的列表没有启用多个选择(或者根本没有选择)。来自:

返回:SparseBooleanArray,对于每次调用get(int position)都返回true,其中position是列表中的一个位置,或者
null
如果选择模式设置为choice\u mode\u NONE


也许您想调用
getCheckedItemPosition()
(不带“s”)?请确保列表选择模式设置为适当的值。

您给了我们三行代码作为第332行。这是哪一行?还有,
list
和can
list.getCheckedItemPositions()
return
null
?@TedHopp check edited question…List是listView…是的,它返回null生成NPE的唯一方法是如果
mSelectedItemIds
null
List
和can
List.getCheckedItemPositions()
return
null
?@TedHopp,由于适配器用于listView,列表引用listView…至于列表。getCheckEditePosition()它确实返回null。你现在怎么办?你给了我们三行代码作为第332行。它是哪一行?还有,什么是
list
和can
list.getCheckEditePositions()
return
null
?@TedHopp check edited question…List是listView…是的,它返回null生成NPE的唯一方法是如果
mSelectedItemIds
null
List
和can
List.getCheckedItemPositions()
return
null
?@TedHopp,由于适配器用于listView,列表引用listView…至于list.getCheckEditPosition()它确实返回null。现在该怎么办?我应该实现
list.setChoiceMode(listView.CHOICE\u MODE\u MULTIPLE)吗
??@user2247689-您不需要实现它;您应该在设置
ListView
时调用它(或者在布局文件中使用
android:choiceMode
属性)。感谢您的帮助…代码现在可以工作了。非常感谢。我应该实现
list.setChoiceMode(ListView.CHOICE\u模式)吗;