0

我使用 setMultiChoiceItems 作为对话框中的列表。单击确定时,代码为

 .setPositiveButton(R.string.alert_remove, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
String[] res = null;
for(int i=-1,l=usrCats.length; ++i<l;){
    if(selections[i])
       res[i] = usrCats[i].toString();
}
if(res != null && res.length>0)
   dbManager.removeUserShoppingCategories(res);
}       
})

我想检查是否选择了一个项目if(selections[i],如果是,则在字符串中添加名称

res[i] = usrCats[i].toString()在最后一个 if 语句中有一个空指针访问和一个死代码警告。

我正在使用最后一个 if 来检查是否有任何选择以调用我的方法。

我究竟做错了什么?

4

1 回答 1

1
        .setPositiveButton(R.string.alert_remove, new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which){
        String[] res = null; // you initialised it with null
        for(int i=-1,l=usrCats.length; ++i<l;){ /*this line and the below line are condition 
    statemnts whose code may not run. so there are chances that your **res** will 
    remain initialised with null*/
            if(selections[i])
               res[i] = usrCats[i].toString();
        }
        if(res != null && res.length>0)// so here it shows a dead code warning.
           dbManager.removeUserShoppingCategories(res);
        }       
        })
于 2012-02-03T11:46:17.467 回答