为什么我在Android项目中收到IndexOutOfBoundsException?

时间:2016-01-28 17:14:59

标签: android android-studio android-spinner indexoutofboundsexception

您好我是android studio的新手,我正在制作注册部分,该项目在手机上正常运行,没有任何错误,但问题是我使用的是旋转器我已经插入了一些值,现在可以选择一些值,如 Rising Star和Ethnically Black and White工作正常,我们可以进一步移动但是只要像 Rnagoli ,logcat显示错误:

01-28 22:23:43.824 23165-23165/myapp.hp.com.abhivyakti E/AndroidRuntime: FATAL EXCEPTION: main
Process: myapp.hp.com.abhivyakti, PID: 23165
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.Arrays$ArrayList.get(Arrays.java:66)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:344)
at android.widget.AdapterView.getItemAtPosition(AdapterView.java:783)
at myapp.hp.com.abhivyakti.Cells.FineArts.FARegisterFragment.onItemSelected(FARegisterFragment.java:115)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:924)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:913)
at android.widget.AdapterView.-wrap1(AdapterView.java)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:883)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

然而在其他部分我使用相同的代码,但Spinner工作正常。我无法弄清楚我的问题的解决方案。 请帮我解决一下这个。我只是给出了相同的代码。

public class FARegisterFragment extends Fragment implements AdapterView.OnItemSelectedListener{


public FARegisterFragment() {
    // Required empty public constructor
}

private Button button;
private String string,string1;
private Spinner sp1,sp2;
private EditText editText1,editText2,editText3,editText4,editText5;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_faregister, container, false);
    button = (Button)view.findViewById(R.id.FineArtsButton);
    sp1 = (Spinner)view.findViewById(R.id.FineArtsSpinner1);
    sp2 = (Spinner)view.findViewById(R.id.FineArtsSpinner2);
    editText1 = (EditText)view.findViewById(R.id.FineArtsEditText1);
    editText2 = (EditText)view.findViewById(R.id.FineArtsEditText2);
    editText3 = (EditText)view.findViewById(R.id.FineArtsEditText3);
    editText4 = (EditText)view.findViewById(R.id.FineArtsEditText4);
    editText5 = (EditText)view.findViewById(R.id.FineArtsEditText5);
    //enables the spinner to be clickable
    sp1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener)this);
    //inputs tge sting into the array
    String[] str = {"Select Event","Rising Star","Ethnically Black and White",
            "Rangoli","Paint and Spray","Theme Painting"};
    //array is been populated into the arrayadapter
    ArrayAdapter<String> aa =new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_dropdown_item,str);
    //dropdown menu vie layout finalised
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //spinner is been assinged the string
    sp1.setAdapter(aa);

    sp2.setOnItemSelectedListener((AdapterView.OnItemSelectedListener)this);
    String[] str1 = {"Select Member","1","2"};
    ArrayAdapter<String> aa1 = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_dropdown_item,str1);
    aa1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp2.setAdapter(aa1);

    //button event
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (string.toString().equals("Select Event")) {
                Toast.makeText(getActivity(),
                        "Event not selected please fill", Toast.LENGTH_LONG).show();
            } else if ((editText1.getText().toString().equals(""))) {
                Toast.makeText(getActivity(),
                        "Contact left please fill", Toast.LENGTH_LONG).show();
            } else if ((editText2.getText().toString().equals(""))) {
                Toast.makeText(getActivity(),
                        "College Name left please fill", Toast.LENGTH_LONG).show();
            } else if (string1.toString().equals("Select Member")) {
                Toast.makeText(getActivity(), "Member not selected please fill", Toast.LENGTH_SHORT).show();
            } else if ((editText3.getText().toString().equals(""))) {
                Toast.makeText(getActivity(),
                        "Participant's Name left please fill", Toast.LENGTH_LONG).show();
            } else if ((editText4.getText().toString().equals("")) || (editText5
                    .getText().toString().equals(""))) {
                Toast.makeText(getActivity(),
                        " Detail Entry left please fill", Toast.LENGTH_LONG).show();
            } else {
                Intent emailintent = new Intent(Intent.ACTION_SEND);
                emailintent.putExtra(Intent.EXTRA_EMAIL,
                        new String[]{"c3club2016@gmail.com"});
                emailintent.putExtra(Intent.EXTRA_SUBJECT, "FINEARTS Registration"
                        + " " + "via Android App");
                emailintent.putExtra(Intent.EXTRA_TEXT,
                        "NAME OF EVENT:" + " " + string + "\n\nPARTICIPANT'S NAME:"
                                + " " + editText3.getText().toString()
                                + "\n\nNUMBER OF PARTICIPANT:" + " " + string1
                                + "\n\nPARTICIPANT'S BRANCH:" + " " + editText4.getText().toString()
                                + "\n\nPARTICIPANT'S YEAR:"
                                + " " + editText5.getText().toString()
                                + "\n\nCOLLEGE NAME:"
                                + " " + editText2.getText().toString() + " "
                                + "\n\nCONTACT:" + " " + editText1.getText().toString());
                emailintent.setType("message/rfc882");
                startActivity(Intent.createChooser(emailintent,
                        "Choose email client"));
            }
        }
    });
    return view;
}
@Override
public void onItemSelected(AdapterView<?> pWRent, View view, int position, long id) {
    string = sp1.getItemAtPosition(position).toString();
    string1 = sp2.getItemAtPosition(position).toString();
}

@Override
public void onNothingSelected(AdapterView<?> pWRent) {

}}

您的帮助将推动我的工作进程,并将为我学习未来提供一些教训。提前谢谢。

1 个答案:

答案 0 :(得分:0)

当您尝试在此处访问位置3时

)$

这不起作用,因为public void onItemSelected(AdapterView<?> pWRent, View view, int position, long id) { string = sp1.getItemAtPosition(position).toString(); string1 = sp2.getItemAtPosition(position).toString(); } 只包含3个元素(索引为0 ... 2)。