Alertdialog ArrayIndexOutOfBoundsException错误

时间:2015-05-21 21:43:53

标签: android arrays exception alertdialog

我试图向AlertDialog显示我从服务器获得的一些数字。显示作品。它也会进入下一个活动。它可以在我的Android设备上运行,但在某些设备上它会出现异常并且手机崩溃。

我总是在logcat中得到这个异常:

  

D / Exception:java.lang.ArrayIndexOutOfBoundsException:length = 0;指数= -1

这是我的代码:

private void showOrderPopup(){
    try{
        materialLoaderOdoo = new MaterialLoaderOdoo();
        dbOrderModel = new DbOrderModel(context);
        Integer partnerId  = dbOrderModel.getPartnerIdByReference(Name);

        final List<Integer> listResults = new ArrayList<Integer>();
        List<Integer> result = dbOrderModel.getOrderById(Integer.parseInt(partnerId.toString()));;
        if(result.size() > 1){
            int t = result.size();
            newArray = new String[result.size()];
            //newArray[0] = "565";
            //newArray[1] = "565";

            for(int d=0;d<result.size();d++){
                listResults.add(d,result.get(d));
            }


            for(int d=0;d<result.size();d++){

                String r = result.get(d).toString();
                newArray[d] = result.get(d).toString();
                System.out.println(newArray[d]);

            }



            AlertDialog.Builder builder =
                    new AlertDialog.Builder(YardActivity.this);
            builder.setTitle("Select One Letter");
            int selected = selectedItem;
            builder.setSingleChoiceItems(
                    newArray,
                    selected,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,int which) {
                            selectedItem=which;
                            System.out.println(which);
                            String nam = listResults.get(which).toString();
                            materialLoaderOdoo.execute(nam);
                            //System.out.println(newArray[selectedItem].toString());
                            Toast.makeText(YardActivity.this,"Loading materials...",Toast.LENGTH_LONG).show();
                            dialog.dismiss();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }else{
            Toast.makeText(YardActivity.this,"Loading materials...",Toast.LENGTH_LONG).show();
            OrderId = listResults.get(0);
            String w = listResults.get(0).toString();
            materialLoaderOdoo.execute(w);
        }
    } catch (Exception e) {
        AlertHandler alertHandler = new AlertHandler(YardActivity.this);
        alertHandler.ShowAlert(YardActivity.this,"Error","Something went wrong!");
        Log.d("Exception", e.toString());

    }
}

任何人都知道如何修复此异常?我尝试了很多东西,但它仍然出现在我的logcat中..

2 个答案:

答案 0 :(得分:0)

当列表大小为0并且我们试图访问列表时,总会出现这种类型的异常......所以你必须检查你正在使用的列表是否有数据..

答案 1 :(得分:0)

打印堆栈跟踪。将帮助您确切地检查哪一行。 要在日志中打印堆栈跟踪,请使用Log.d(TAG, MESSAGE, new Exception());