Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 数组问题:ArrayIndexOutOfBoundsException_Android_Arrays_Listview_Sharedpreferences_Counter - Fatal编程技术网

Android 数组问题:ArrayIndexOutOfBoundsException

Android 数组问题:ArrayIndexOutOfBoundsException,android,arrays,listview,sharedpreferences,counter,Android,Arrays,Listview,Sharedpreferences,Counter,我试图将作为输入(字符串)的变量存储在resultsArray中。出于某种原因,填补下一个空缺的工作没有进展。结果数组中只有第一个点起作用。有什么想法吗?正如您在if placecounter==0中看到的那样,我每次都存储字符串元素,然后再次调用它(placecounter>0),并尝试向其中添加一个元素 if(placeCounterExplanation == 0){ CategoryDetailArray = new String[30];

我试图将作为输入(字符串)的变量存储在resultsArray中。出于某种原因,填补下一个空缺的工作没有进展。结果数组中只有第一个点起作用。有什么想法吗?正如您在if placecounter==0中看到的那样,我每次都存储字符串元素,然后再次调用它(placecounter>0),并尝试向其中添加一个元素

 if(placeCounterExplanation == 0){
                 CategoryDetailArray = new String[30];
                 CategoryDetailArray[0] = categoriesItem;

                 IndividualExpenseDetailArray = new String[30];
                 IndividualExpenseDetailArray[0] = individualamountitem;

                 ExplanationDetailArray = new String[30];
                 ExplanationDetailArray[0] = explanationItem;

                 DateofPurchaseArray = new String [30];
                 DateofPurchaseArray[0] = currentDateItem;
                // SharedPreferences sp = SharedPreferences.getDefaultSharedPreferences(this);
                //    SharedPreferences.Editor mEdit1 = sp.edit();
                 test.setText(ExplanationDetailArray[0]);
                 test2.setText(IndividualExpenseDetailArray[0]);
                // test3.setText(DateofPurchaseArray[0]);
                 test4.setText(CategoryDetailArray[0]);


                 //String placeCount = Integer.toString(placeCounterExplanation);

                //  test3.setText(placeCount);


                 //STORE
                    editor.putInt("Status_size", ExplanationDetailArray.length); /* sKey is an array */ 

                    for(int i=0;i<ExplanationDetailArray.length;i++)  
                    {
                        editor.remove("Explanation_" + i);
                        editor.remove("ExpenseAmount_" + i);
                        editor.remove("Category_" + i);
                        editor.remove("DOP_" + i);
                        editor.putString("Explanation_" + i, ExplanationDetailArray[i]);  
                        editor.putString("ExpenseAmount_" + i, IndividualExpenseDetailArray[i]);  
                        editor.putString("Category_" + i, CategoryDetailArray[i]);  
                        editor.putString("DOP_" + i, DateofPurchaseArray[i]);  
                    }

                     editor.commit();     
                }






             if(placeCounterExplanation > 0){

                 //SharedPreferences mSharedPreference1 = PreferenceManager.getDefaultSharedPreferences(this);


                 //RECEIVE
                   // ExplanationDetailArray.clear();
                    int size = pref.getInt("Status_size", 0);  






                    for(int i=0;i<size;i++) 
                    {
                       // ExplanationDetailArray.add(pref.getString("Explanation" + i, null));
                        ExplanationDetailArray[i] = pref.getString("Explanation_" + i, null);
                        IndividualExpenseDetailArray[i] = pref.getString("ExpenseAmount_" + i, null);
                        CategoryDetailArray[i] = pref.getString("Category_" + i, null);
                        DateofPurchaseArray[i] = pref.getString("DOP_" + i, null);
                    }

                    ExplanationDetailArray[placeCounterExplanation] = explanationItem;
                    IndividualExpenseDetailArray[placeCounterExplanation] = individualamountitem;
                    CategoryDetailArray[placeCounterExplanation] = categoriesItem;
                    DateofPurchaseArray[placeCounterExplanation] = currentDateItem;

                    Intent intent = getIntent();
                    // getting attached intent data
                    String product = intent.getStringExtra("product");
                    // displaying selected product name
                     txtProduct.setText(product);



                    String[] ResultsArray = new String[placeCounterExplanation];




                    if (product.equals("Alcohol")){
                        //test3.setText(product);

                        if(CategoryDetailArray[0].equals("Alcohol")){
                            ResultsArray[resultCounter] = (IndividualExpenseDetailArray[resultCounter] + "" + ExplanationDetailArray[resultCounter] + "" + DateofPurchaseArray[resultCounter] );
                        }

                        if(CategoryDetailArray[1].equals("Alcohol")){
                            ResultsArray[resultCounter + 1] = (IndividualExpenseDetailArray[resultCounter + 1] + "" + ExplanationDetailArray[resultCounter + 1] + "" + DateofPurchaseArray[resultCounter + 1] );
                        }

                        //  resultCounter = resultCounter + 1;
                    //}
                        test.setText(ResultsArray[0]);
                         test2.setText(ResultsArray[1]);

                        //editor.putInt("resultCounter",resultCounter);
                        // editor.commit();

                    //arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ResultsArray);
                //  resultsListView.setAdapter(arrayAdapter);




            //building result array
                  /*    for(int i=0; i< placeCounterExplanation;i++){

                            if(CategoryDetailArray[i].equals("Alcohol")){

                                int resultCounter = pref.getInt("resultCounter",0);

                                    ResultsArray[resultCounter] = (IndividualExpenseDetailArray[resultCounter] + "" + ExplanationDetailArray[resultCounter] + "" + DateofPurchaseArray[resultCounter] );
                                    resultCounter = resultCounter + 1;

                                     editor.putInt("resultCounter",resultCounter);
                                     editor.commit();
                            }

                        }   
                    }
if(placeCounterExpression==0){
CategoryDetailArray=新字符串[30];
CategoryDetailArray[0]=类别项;
IndividualExpenseDetailArray=新字符串[30];
IndividualExpenseDetailArray[0]=individualamountitem;
ExplanationDetailArray=新字符串[30];
ExplanationDetailArray[0]=explanationItem;
DateofPurchaseArray=新字符串[30];
DateofPurchaseArray[0]=当前日期项;
//SharedReferences sp=SharedReferences.GetDefaultSharedReferences(此);
//SharedReferences.Editor mEdit1=sp.edit();
test.setText(ExplanationDetailArray[0]);
test2.setText(IndividualExpenseDetailArray[0]);
//test3.setText(DateofPurchaseArray[0]);
test4.setText(categorydeailarray[0]);
//字符串placeCount=Integer.toString(placeCounter解释);
//test3.setText(placeCount);
//贮藏
putInt(“Status_size”,ExplanationDetailArray.length);/*sKey是一个数组*/
对于(int i=0;i 0){
//SharedPreferences mSharedPreference1=PreferenceManager.getDefaultSharedPreferences(此);
//接收
//ExplanationDetailArray.clear();
int size=pref.getInt(“状态大小”,0);

对于(int i=0;i您可以使用ArrayList,这样就没有添加项的限制

ArrayList<String> yourlistname = new ArrayList<String>();
为了获得价值

String value = yourlistname.get(index).toString();
用于获取ArrayList的大小

int length = yourlistname.size();

这是一整段代码。你能把它简化一下吗?数组的长度是30,但你填充了其中的1个。使用arrayList代替它。arrayList x=new arrayList();所以问题出在if(placeCounterExpression>0)中的某个地方,它没有像我尝试的那样添加新元素(whichearray[placeCounterExpression]我是新来的,没有任何使用arrayList的经验。你能解释更多并提供帮助吗me@user3924167如何将其存储在shared prefI中?我现在将尝试实现此功能。基于我已有的功能,这将需要后退几步。我感谢您的帮助,并将根据我已有的功能立即尝试此功能您是否看到任何可能导致数组indexOutofBoundsException的问题?我能够在我的方法中找到问题。谢谢您。我检查了复选标记。我没有15个声誉,因此无法投票。
String value = yourlistname.get(index).toString();
int length = yourlistname.size();