IndexOutOfBoundsException SharedPreferences

问题内容:

I read many posts even though I didn’t get the solution.My Scenario is,

In My activity1 (first activity of tabhost) , I have a shared preferences in which I have stored a string set as,

Set<String> arr = new HashSet<String>();
arr.add("term");
arr.add(((EditText) scrollView.findViewById(R.id.loan)).getText().toString());
arr.add(((EditText) scrollView.findViewById(R.id.emi)).getText().toString());
arr.add(((EditText) scrollView.findViewById(R.id.interest)).getText().toString());
SharedPreferences.Editor edit = savedloan.edit();
edit.putStringSet(input.getText().toString(), arr);//String in input is always unique
edit.commit();

I am using listview to show that data sharedpreferences in another activity as follows,

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = activity.getLayoutInflater();
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.savedlistviewitem, null);
        holder = new ViewHolder();
        holder.txtFirst =  (TextView) convertView.findViewById(R.id.textView95);
        holder.button=(ImageButton)convertView.findViewById(R.id.imageButton5);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.txtFirst.setText(list.get(position).toString());
    ((ImageButton)convertView.findViewById(R.id.imageButton5)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences.Editor editor = EMI_calculation.savedloan.edit();
            editor.remove(list.get(position).toString());
            Toast.makeText(activity, "\"" + list.get(position).toString() + "\" was deleted Successfully", Toast.LENGTH_SHORT).show();
            list.remove(position);
            editor.commit();
            MyListAdapter.this.notifyDataSetChanged();

        }
    });
    ((TextView)convertView.findViewById(R.id.textView95)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TabHostPage.TabHostWindow.setCurrentTab(0);
            try {
                new activity1().calltabone(((TextView) v).getText().toString());
            }
            catch (Exception e)
            {
                Log.d("exception in method call",e.toString());
            }
        }
    });
    return convertView;
}

When the user clicks the textview, it call the method of activity1.Code for that method is

public void calltabone(String a)
{
    try {
        Set<String> ab = savedloan.getStringSet(a, null);
        Iterator iter = ab.iterator();
        String[] array = new String[5];
        int i = 0;
        do {
            array[i] = iter.next().toString();
            i++;
        } while (iter.hasNext());
        ((EditText) scrollView.findViewById(R.id.loan)).setText(array[0]);
        ((EditText) scrollView.findViewById(R.id.interest)).setText(array[1]);
        ((EditText) scrollView.findViewById(R.id.term)).setText(array[2]);
        ((Spinner) scrollView.findViewById(R.id.spinner2)).setSelection(Integer.parseInt(array[3]));
        (scrollView.findViewById(R.id.calculate)).setOnClickListener(calculatelistener);
        (scrollView.findViewById(R.id.reset)).setOnClickListener(resetlistener);
        ((Button) scrollView.findViewById(R.id.calculate)).performClick();
    }
    catch (Throwable e)
    {
        e.printStackTrace();
        Log.d("error in method",e.toString());
    }
}

Problem is I am getting unrelated exceptions in my log,

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.thiru.financial_calculator, PID: 17737
  java.lang.IndexOutOfBoundsException: Invalid index 12, size is 2
  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
  at java.util.Arrays$ArrayList.get(Arrays.java:66)
  at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390)
  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
  at android.widget.Spinner.makeView(Spinner.java:617)
  at android.widget.Spinner.layout(Spinner.java:565)
  at android.widget.Spinner.onLayout(Spinner.java:527)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
  at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
  at android.widget.ScrollView.onLayout(ScrollView.java:1505)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
  at android.view.View.layout(View.java:15689)
  at android.view.ViewGroup.layout(ViewGroup.java:5040)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
  at android.view.View.layout(View.java:15689)
  at andro

Please take a look at this and help me! I tried throwable in catch by referring other posts. Thanks in Advance

问题评论:

    
As I can see in your posted error message you have an IndexOutOfBoundsException. When I check your code it seems you loop always an array with size 5 (String[] array = new String[5];). But your iterator (Iterator iter = ab.iterator();) may not have that many entries.
    
What do you edit at list.remove(position);? Where is this list coming from?
    
@BrunoBieri The list has the keys (unique name) of savedloan sharedpreferences. I am just showing the name only and (Iterator iter = ab.iterator();) may not have that many entries. I have added in top of my question what I am storing in savedloan.
– Joe
4 hours ago
    
At which line does the code stop (throws an exception) when you debug your application?

原文地址:

https://stackoverflow.com/questions/47752222/indexoutofboundsexception-sharedpreferences

添加评论

友情链接:蝴蝶教程