ArrayIndexOutOfBoundsException但Array不为空

时间:2015-11-27 09:29:53

标签: android arrays fragment

我收到了以下错误消息java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 at com.sbeyer.daainv.EditDataFragment.onCreateView(EditDataFragment.java:110)我知道这个错误表明我的数组只包含[0]值。但是我片段中的Log.d消息给了我这个日志:

11-27 09:50:18.383 21696-21696/com.sbeyer.daainv D/dataLength: 9
11-27 09:50:18.383 21696-21696/com.sbeyer.daainv D/[0]: 65
11-27 09:50:18.383 21696-21696/com.sbeyer.daainv D/[1]: Drucker
11-27 09:50:18.383 21696-21696/com.sbeyer.daainv D/[2]: WSN-0020
11-27 09:50:18.383 21696-21696/com.sbeyer.daainv D/[3]: 651615231
11-27 09:50:18.383 21696-21696/com.sbeyer.daainv D/[4]: 65:65:65:65:65:65
11-27 09:50:18.384 21696-21696/com.sbeyer.daainv D/[5]: 013665161523
11-27 09:50:18.384 21696-21696/com.sbeyer.daainv D/[6]: 20.01.2015
11-27 09:50:18.384 21696-21696/com.sbeyer.daainv D/[7]: 1
11-27 09:50:18.384 21696-21696/com.sbeyer.daainv D/[8]: 1

你看我的数组不是空的......还有其他的东西,比如将值设置为EditTexts,工作正常。但是,如果我点击fabmain错误发生(我的DBHelper类中调用的每个方法也正常工作。更改会保存,但我的应用程序会崩溃。有人可以帮助我吗?

这是我的EditDataFragment

public class EditDataFragment extends Fragment {
DbHelper mydb;

[...]

private String currentLeid;
private String currentProduct;

boolean firstFocus;

//Validator booleans
boolean labelOk;
boolean serialOk = true;
boolean macOk;
boolean daaIdOk;
boolean billOk;
boolean locationOk;
boolean roomOk;

boolean checkFirst;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_edit_data, container, false);

    getActivity().setTitle(getString(R.string.Add_Element_ActionBar));

    setRetainInstance(true);

    final Context context = getContext();
    mydb = new DbHelper(context);
    firstFocus = false;
    checkFirst = true;

    Drawable drawableForFabDone = getResources().getDrawable(R.drawable.ic_done);
    ((MainActivity) getActivity()).fabmain.setImageDrawable(drawableForFabDone);

    inputLabel = (EditText) view.findViewById(R.id.editText_label);
    inputSerial = (EditText) view.findViewById(R.id.editText_serial);
    inputMac = (EditText) view.findViewById(R.id.editText_mac);
    inputDaaId = (EditText) view.findViewById(R.id.editText_daa_id);
    inputBill = (EditText) view.findViewById(R.id.editText_date);
    inputLocation = (EditText) view.findViewById(R.id.editText_location);
    inputRoom = (EditText) view.findViewById(R.id.editText_room);

    checkFabMain(inputLabel);


    Spinner products = (Spinner) view.findViewById(R.id.spinner_products);
    SimpleImageArrayAdapter adapter = new SimpleImageArrayAdapter(context,
            new Integer[]{R.drawable.icon_pc_circle_128, R.drawable.icon_laptop_circle_128, R.drawable.icon_printer_circle_128, R.drawable.icon_other});
    products.setAdapter(adapter);

    //Fill EditTexts if "Edit" is clicked in ListViewFragment///////////////////////////////////////////////////////////

    if ((((MainActivity) getActivity()).leidToEdit) != "") ;
    {
        String Leid = (((MainActivity) getActivity()).leidToEdit);
        String dataToEdit = mydb.getElementsById(Leid);
        String[] dataToEditArray = dataToEdit.split("\t");
        checkFirst = false;


        currentLeid = dataToEditArray[0];

        Log.d("dataLength", Integer.toString(dataToEditArray.length));
        Log.d("[0]", dataToEditArray[0]);
        Log.d("[1]", dataToEditArray[1]);    <<<<<<<<<<<<<<<<<<<<<<<<LINE110
        Log.d("[2]", dataToEditArray[2]);
        Log.d("[3]", dataToEditArray[3]);
        Log.d("[4]", dataToEditArray[4]);
        Log.d("[5]", dataToEditArray[5]);
        Log.d("[6]", dataToEditArray[6]);
        Log.d("[7]", dataToEditArray[7]);
        Log.d("[8]", dataToEditArray[8]);

        currentProduct = dataToEditArray[1];

        checkSpinner(dataToEditArray, products);

        inputLabel.setText(dataToEditArray[2]);
        labelOk = true;
        inputSerial.setText(dataToEditArray[3]);
        serialOk = true;
        inputMac.setText(dataToEditArray[4]);
        macOk = true;
        inputDaaId.setText(dataToEditArray[5]);
        daaIdOk = true;
        inputBill.setText(dataToEditArray[6]);
        billOk = true;

        String LidFromArray = dataToEditArray[7];
        inputLocation.setText(mydb.getLocationById(LidFromArray));
        if (inputLocation.getText().toString().length() == 4) {
            locationOk = true;
        }

        String RidFromArray = dataToEditArray[8];
        inputRoom.setText(mydb.getRoomById(RidFromArray));
        if (inputRoom.getText().toString().length() == 3) {
            roomOk = true;
        }


        checkFabMain(inputLabel);
    }
}


//Initialize the input methods//////////////////////////////////////////////////////////////////////////////////////
[...]


        ((MainActivity) getActivity()).fabmain.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Context context = getContext();

        String product = inputProduct;
        String label = inputLabel.getText().toString();
        String serial = inputSerial.getText().toString();
        String mac = inputMac.getText().toString();
        String daaid = inputDaaId.getText().toString();
        String bill = inputBill.getText().toString();
        String location = inputLocation.getText().toString();
        String room = inputRoom.getText().toString();

        mydb.deleteByLeid(currentLeid);
        mydb.setData(product, label, serial, mac, daaid, bill);


        currentLeid = "";

        Toast toast = Toast.makeText(context, "Datensatz bearbeitet", Toast.LENGTH_SHORT);
        toast.show();


        ((MainActivity) getActivity()).setLeidToEditZero();

        loadSavedRoomsFragment();

    }
});


((MainActivity) getActivity()).fabdelete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        inputLabel.setText("");
        inputSerial.setText("");
        inputMac.setText("");
        inputDaaId.setText("");
        inputBill.setText("");

        firstFocus = true;
        ((MainActivity) getActivity()).fabdelete.animate().translationY(375);
    }
});
return view;

似乎onCreateView方法运行了两次。但我无法弄清楚原因。

logcat的:

11-27 10:37:27.193 24915-24915/com.sbeyer.daainv E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.sbeyer.daainv, PID: 24915
                                                               java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
                                                                   at com.sbeyer.daainv.EditDataFragment.onCreateView(EditDataFragment.java:109)
                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                   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)

如果我加载数组一切都很好(请参阅上面的Log.d条目)dataLength: 9我点击我的按钮后我从该位置获得另一个Log.d条目dataLength: 1为什么我的片段和#34;重建&#34;本身?

2 个答案:

答案 0 :(得分:2)

错误说length=1; index=1。这意味着,您的数组不是空的,有一个项目,但您尝试获取第二个项目。数组以0索引开头,而不是1.这就是问题。

答案 1 :(得分:0)

问题解决了。片段管理器在加载另一个片段时出现问题。