2013-08-26 92 views
0

與複選框工作列表視圖中產生上下文動作條是給我下面的空指針異常:空指針異常適配器getView

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; 
} 

線332

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

這是在適配器

+0

你給了我們三行代碼作爲行332.哪一行?另外,什麼是'list'並且可以'list.getCheckedItemPositions()'返回'null'? –

+0

@TedHopp檢查編輯的問題...列表是listView。 ... yes返回null –

+1

生成NPE的唯一方法是如果'mSelectedItemIds'爲'null'。什麼是'list'並且可以'list.getCheckedItemPositions()'return'null'? –

回答

3

這個問題似乎是你的名單並不適用於多個選擇啓用(或選擇可言,顯然)的getView()。從the docs for getCheckedItemPositions()

返回:SparseBooleanArray將每個呼叫返回true得到(INT位置)位置是在列表中的位置,或者null如果選擇模式設置爲CHOICE_MODE_NONE。

也許你打算打電話getCheckedItemPosition()(沒有「s」)?確保列表選擇模式設置爲適當的值。

+0

我應該實現'list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);'??? –

+0

@ user2247689 - 您不需要_implement_;你應該在設置ListView時調用它(或者在使用android:choiceMode屬性的佈局文件中)。 –

+0

感謝您的幫助...代碼現在正在工作。非常感謝你。 –