Spinner上的Android IndexOutOfBounds错误

时间:2016-12-04 21:19:01

标签: java android android-studio

我正在开发一个带有Activity的Android应用程序,它将DynamoDB中的数据加载到Spinner中。我通过调用employeeSpinner = (Spinner)findViewById(R.id.employeeSpinner);来设置Spinner,并通过从DynamoDB加载列表来设置其适配器和数据,如下所示:

final Context context = this;
    AsyncHandler<ScanRequest, ScanResult> asyncHandler = new AsyncHandler<ScanRequest, ScanResult>() {
        public void onError(Exception exception) {
            showDialogMessageAsync("Error", "Error scanning table: " + exception.getMessage());
        }

        @Override
        public void onSuccess(ScanRequest request, ScanResult result) {
            for (Map<String, AttributeValue> data : result.getItems()) {
                ConcurrentHashMap hashData = new ConcurrentHashMap<>(data);
                String orgName = (hashData.get(DynamoDBManager.ORGANIZATION_NAME)).toString();
                String employeeName = (hashData.get(DynamoDBManager.EMPLOYEE_NAME)).toString();
                Employee e = new Employee(orgName, employeeName);
                employeeList.add(e);
            }
            ArrayList<String> employeeNames = new ArrayList<>();
            int length = employeeList.size();
            for(int i = 0; i < length; i++){
                String name = employeeList.get(i).getEmployeeName();
                employeeNames.add(name);
            }
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, employeeNames);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            employeeSpinner.setAdapter(dataAdapter);
        }
    };
    HashMap<String, AttributeValue> attributeValues = new HashMap<>();
    attributeValues.put(":orgName", new AttributeValue().withS(getOrgName()));
    String filterExpression = "organization_name = :orgName";
    DynamoDBManager.scan(asyncClient, DynamoDBManager.EMPLOYEE_TABLE, filterExpression, attributeValues, asyncHandler);

以下是创建微调器后发生的崩溃的堆栈跟踪:

java.lang.ArrayIndexOutOfBoundsException: length=15; index=-1
                  at java.util.ArrayList.get(ArrayList.java:413)
                  at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:349)
                  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:405)
                  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)
                  at android.widget.Spinner.makeView(Spinner.java:702)
                  at android.widget.Spinner.layout(Spinner.java:650)
                  at android.widget.Spinner.onLayout(Spinner.java:612)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                  at android.view.Choreographer.doFrame(Choreographer.java:619)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我觉得特别奇怪的是它说的是index = -1,但是在创建它之后我不会对我的Spinner做任何事情,所以我不确定是什么会导致尝试访问它指数。但是,长度似乎是我的列表的预期长度。可能导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:0)

Einar的评论解决了我的问题 - 我所要做的就是将我的代码包装在runOnUiThread中并且不再发生崩溃:

runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    employeeSpinner.setAdapter(dataAdapter);
                }
            });