死代码 android setMultiChoiceItems

发布于 2025-01-02 08:20:37 字数 670 浏览 4 评论 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] 选择了某个项目,如果是,请将名称添加到 String

i 在 res[i] = usrCats[i].toString() 中具有空指针访问权限,并且在最后一个 if 语句中出现死代码警告。

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

我做错了什么?

i use setMultiChoiceItems for a list in a dialog.
when click on ok the code is

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

i want to check if an item is selected with if(selections[i] and if so, add the name in a
String

i am having a null pointer access in the res[i] = usrCats[i].toString() and a dead code warning in the last if statement.

i am using the last if, to check if there has been any selection so as to call my method.

what am i doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2025-01-09 08:20:37
        .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);
        }       
        })
        .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);
        }       
        })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文