0

所以我加入了一个目前正在编写 android 的团队,所以有一个问题会导致应用程序崩溃。这是由于 arraylist 超出范围。代码在下面,失败是代码的最后一点(该类在此之后继续进行。)。我希望有人能指出确切的问题及其解决方案,不会破坏其他任何东西:

public class OneClickEssentialExtrasFragment extends BaseOneClickFragment {

    public static final String TAG = "PhoneOneClickEssentialExtrasFragment";

    private View myView;

    Map<Integer, Integer> stockProductMap;

    Product mProducts;

    private List<Stock> mStockCollection;

    private List<Stock> mStockDelivery;

    private List<Integer> mStockProductId;

    ArrayList<String> myEssenatialList;

    Map<String, Product> myProduct;

    private String deliveryZipCode = "EC3M3BD";

    WebServiceFacade mWebManager;

    private int ESSENTIAL_EXTRA_PRODUCT_ONE = 0;

    private int ESSENTIAL_EXTRA_PRODUCT_TWO = 1;

    private int ESSENTIAL_EXTRA_PRODUCT_THREE = 2;

    private int currentState = 0;

    private static final int STATE_LOADING = 0;

    private static final int STATE_DISPLAY_ESSENTIAL_EXTRAS = 1;

    public static  String mreservedProduct="";


    public View onCreateView(final LayoutInflater inflater,
            final ViewGroup container, final Bundle savedInstanceState) {
        return inflater.inflate(
                R.layout.fragment_one_click_essential_extras_list, container,
                false);
    }

    public void onViewCreated(final View view, final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        myView = view;
        currentState = STATE_LOADING;
        Bundle b = getActivity().getIntent().getExtras();
        Product mProduct = b.getParcelable("product");
        mreservedProduct =mProduct.getReference();
        mWebManager = WebServiceFacade.getInstance();
        myProduct = new HashMap<String, Product>();
        Bundle bundle = getActivity().getIntent().getExtras();
        mProducts = bundle.getParcelable("product");

        mStockCollection = new ArrayList<Stock>();
        mStockDelivery = new ArrayList<Stock>();
        mStockProductId = new ArrayList<Integer>();
        mStockProductId.add(Integer.parseInt(mProduct.getReference()));
        // To-do
        myEssenatialList = new ArrayList<String>();
        myEssenatialList.add(mProduct.getReference());
        renderEssentialScreen();
        if (myEssenatialList == null || myEssenatialList.size() == 0) {
            // myView.findViewById(R.id.essential_extras_item_icon_container)
            // .setVisibility(View.GONE);
            resetEssentialViews();
            return;
        }

        if (mProducts != null) {
            for (int i = 0; i < myEssenatialList.size(); i++) {
                mWebManager.getProduct(mProductDetailsCallback,
                        myEssenatialList.get(i));
            }
        } else {

        }
    }

    private void checkStockCollection(Product mProduct, int quantitiesselected) {

        int storeId = 0;
        if(mArgosApplication.getCurrentStore()!=null)
            storeId = mArgosApplication.getCurrentStore().getStoreNumber();

        List<String> products = new ArrayList<String>();
        //TO DO: Get the products list from Order API and add here
        products.add(mProduct.getReference());
        mWebManager.checkStock(mStockCheckCallbackCollection,quantitiesselected, storeId, products,
                deliveryZipCode);
    }

    private final UICallback<Map<Integer, Map<Integer, Stock>>> mStockCheckCallbackCollection = new BaseUICallback<Map<Integer, Map<Integer, Stock>>>() {

        @Override
        protected void handlePlannedMaintenance(final int errorCode,
                final String errorMessage) {
            QLog.d("ignored stock lookup 503 error: " + errorMessage);
        }


        @Override
        public void onSuccess(final Map<Integer, Map<Integer, Stock>> data) {
            if (data != null && mProducts != null) {
                Map<Integer, Stock> productStockMap = null;
                if(mArgosApplication.getCurrentStore()!=null)
                    productStockMap = data.get(mArgosApplication.getCurrentStore().getStoreNumber());
                else // Global stock, see NewStockHandler.java ->convertStockToMap is constructing the map with 1000, in case store URI is null
                    productStockMap = data.get(1000);
                if (productStockMap != null) {
                    for (int index = 0; index < productStockMap.size(); index++) {
                        if (mStockProductId != null) {
                            mStockCollection.add(productStockMap
                                    .get(mStockProductId.get(index)));
                            currentState = STATE_DISPLAY_ESSENTIAL_EXTRAS;
                            renderEssentialScreen();
                        }
                    }
                }
            } else {
                QLog.d("No Response for Product");
            }
        }


        @Override
        public boolean handleFailure(int errorCode, String errorMessage) {
            QLog.d("503 Stock Check not Available");
            return false;
        }
    };

    private void renderEssentialScreen() {
        if (myEssenatialList == null || myEssenatialList.size() == 0) {
            resetEssentialViews();
        }
        switch (currentState) {
        case STATE_LOADING:
            renderProgressBar();
            break;
        case STATE_DISPLAY_ESSENTIAL_EXTRAS:
            renderEssentialData();
            break;
        }
    }

    private void renderProgressBar() {
        resetEssentialViews();
        myView.findViewById(R.id.progressbar).setVisibility(View.VISIBLE);
    }

    private void renderEssentialData() {
        myView.findViewById(R.id.progressbar).setVisibility(View.GONE);
        if (myEssenatialList == null || myEssenatialList.size() == 0) {
            resetEssentialViews();
            return;
        }
        resetEssentialViews();
        if (myEssenatialList.size() == ESSENTIAL_EXTRA_PRODUCT_ONE) {
            fillProductFirst();
        } else if (myEssenatialList.size() == ESSENTIAL_EXTRA_PRODUCT_TWO) {
            fillProductFirst();
            fillProductMiddle();
        } else {
            fillProductFirst();
            fillProductMiddle();
            fillProductLast();
        }
    }

    private void resetEssentialViews() {
        myView.findViewById(R.id.essential_extras_one).setVisibility(View.GONE);
        myView.findViewById(R.id.essential_extras_two).setVisibility(View.GONE);
        myView.findViewById(R.id.essential_extras_three).setVisibility(
                View.GONE);
    }

    private void fillProductMiddle() {

        if (isAdded() == false) {
            return;
        }


        System.out.println("size of array = " + myEssenatialList.size());
        for(int i =0; i <myEssenatialList.size(); i++){
            System.out.println("elements of array are: " + i);
        }

        myView.findViewById(R.id.essential_extras_two).setVisibility(View.VISIBLE);
        TextView productDescription = (TextView)myView.findViewById(R.id.product_name_two);
        productDescription.setText(myProduct.get(myEssenatialList.get(ESSENTIAL_EXTRA_PRODUCT_TWO))
                .getShortDescription());

谢谢。

更新:

blow 是注销输出:

10-16 08:19:11.280: E/AndroidRuntime(23119): FATAL EXCEPTION: main
10-16 08:19:11.280: E/AndroidRuntime(23119): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
10-16 08:19:11.280: E/AndroidRuntime(23119):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at java.util.ArrayList.get(ArrayList.java:308)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.fragment.pdp.OneClickEssentialExtrasFragment.fillProductMiddle(OneClickEssentialExtrasFragment.java:352)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.fragment.pdp.OneClickEssentialExtrasFragment.renderEssentialData(OneClickEssentialExtrasFragment.java:312)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.fragment.pdp.OneClickEssentialExtrasFragment.renderEssentialScreen(OneClickEssentialExtrasFragment.java:287)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.fragment.pdp.OneClickEssentialExtrasFragment.access$4(OneClickEssentialExtrasFragment.java:278)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.fragment.pdp.OneClickEssentialExtrasFragment$1.onSuccess(OneClickEssentialExtrasFragment.java:252)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.fragment.pdp.OneClickEssentialExtrasFragment$1.onSuccess(OneClickEssentialExtrasFragment.java:1)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.webservices.WebServiceExecutor$WebMethodTask.onPostExecute(WebServiceExecutor.java:466)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.webservices.WebServiceExecutor$WebMethodTask.onPostExecute(WebServiceExecutor.java:1)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.os.AsyncTask.finish(AsyncTask.java:631)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.os.Looper.loop(Looper.java:137)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at android.app.ActivityThread.main(ActivityThread.java:5103)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at java.lang.reflect.Method.invokeNative(Native Method)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at java.lang.reflect.Method.invoke(Method.java:525)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-16 08:19:11.280: E/AndroidRuntime(23119):    at dalvik.system.NativeStart.main(Native Method)
10-16 08:19:17.910: E/Google Maps Android API(23119): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).
4

2 回答 2

1

这是一个简单的错误。

你这样做:

myProduct.get(myEssenatialList.get(ESSENTIAL_EXTRA_PRODUCT_TWO))

在哪里:

private int ESSENTIAL_EXTRA_PRODUCT_TWO = 1;

如果您的列表只有 1 个元素怎么办?在这种情况下,您想要第 0 项。尝试访问第一个元素将引发越界异常。

于 2013-10-16T07:40:11.067 回答
0

根据您的崩溃堆栈跟踪。

在 java.util.ArrayList.get(ArrayList.java:308) 10-16 08:19:11.280: E/AndroidRuntime(23119):

android.fragment.pdp.OneClickEssentialExtrasFragment.fillProductMiddle(OneClickEssentialExtrasFragment.java:352)

我相信是这条线

productDescription.setText(myProduct.get(myEssenatialList.get(ESSENTIAL_EXTRA_PRODUCT_TWO))
                .getShortDescription());

您应该调试此行并将其拆分为单独的数组访问..因为您在单个语句中链接了两个 get,所以调试哪个数组被越界访问是很困难的。拆分后,您可以调试超出范围的访问以及原因。

于 2013-10-16T07:40:29.890 回答