Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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
Java Android IndexOutOfBoundsException索引无效,大小为0_Java_Android_Arrays_Json - Fatal编程技术网

Java Android IndexOutOfBoundsException索引无效,大小为0

Java Android IndexOutOfBoundsException索引无效,大小为0,java,android,arrays,json,Java,Android,Arrays,Json,我正在创建一个具有“添加到购物车”功能的应用程序。我遵循这一点,效果很好,但是当我尝试在产品配置文件中而不是在listview中添加“添加到购物车”时,我得到了一个错误,它是IndexOutOfBoundsException无效索引51,大小为0 ProductDetails.java public class ProductDetails extends Fragment { static ConnectivityManager cm; AlertDialog dialog2

我正在创建一个具有“添加到购物车”功能的应用程序。我遵循这一点,效果很好,但是当我尝试在产品配置文件中而不是在listview中添加“添加到购物车”时,我得到了一个错误,它是IndexOutOfBoundsException无效索引51,大小为0

ProductDetails.java

public class ProductDetails extends Fragment {

    static ConnectivityManager cm;
    AlertDialog dialog2;
    AlertDialog.Builder build;

    TextView txtproduct,
             txtprice,
             txtcategorytype,
             txtpieces,
             txtdescription;

    ImageView imgimage;

    Button btncart,
           btnview;

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

        final Controller ct = (Controller) getActivity().getApplicationContext();

        cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);// checking
        build = new AlertDialog.Builder(getActivity()); // connectivity

        // Create default options which will be used for every
        //  displayImage(...) call if no options will be passed to this method
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity())
                .defaultDisplayImageOptions(defaultOptions)
                .build();

        if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)// if connection is
                // there screen goes
                // to next screen
                // else shows
                // message
                .isConnectedOrConnecting()
                || cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                .isConnectedOrConnecting()) {
            Log.e("cm value",
                    ""
                            + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                            .isConnectedOrConnecting());

            int teaProfileId = getArguments().getInt("teaProfileId");
            String teaProfileName = getArguments().getString("teaProfileName");
            Float teaProfilePrice = getArguments().getFloat("teaProfilePrice");
            String teaProfileImage = getArguments().getString("teaProfileImage");
            String teaProfileLabel = getArguments().getString("teaProfileLabel");
            int teaProfilePieces = getArguments().getInt("teaProfilePieces");
            String teaProfilePiecesType = getArguments().getString("teaProfilePiecesType");
            String teaProfileDescription = getArguments().getString("teaProfileDescription");


            try {
                JSONObject jsonObject1 = new JSONObject(String.valueOf(teaProfileId));
                JSONObject jsonObject2 = new JSONObject(teaProfileName);
                JSONObject jsonObject3 = new JSONObject(Float.toString(teaProfilePrice));
                JSONObject jsonObject4 = new JSONObject(teaProfileImage);
                JSONObject jsonObject5 = new JSONObject(teaProfileLabel);
                JSONObject jsonObject6 = new JSONObject(String.valueOf(teaProfilePieces));
                JSONObject jsonObject7 = new JSONObject(teaProfilePiecesType);
                JSONObject jsonObject8 = new JSONObject(teaProfileDescription);


            } catch (JSONException e) {
                e.printStackTrace();
            }

            txtproduct = (TextView) rootView.findViewById(R.id.txtproduct);
            txtprice = (TextView) rootView.findViewById(R.id.txtprice);
            txtcategorytype = (TextView) rootView.findViewById(R.id.txtcategorytype);
            txtpieces = (TextView) rootView.findViewById(R.id.txtpieces);
            txtdescription = (TextView) rootView.findViewById(R.id.txtdescription);

            txtproduct.setText(teaProfileName);
            txtprice.setText(("PHP ") + String.format("%.2f", teaProfilePrice));
            txtcategorytype.setText(teaProfileLabel);
            txtpieces.setText((teaProfilePieces)+" "+(teaProfilePiecesType));
            txtdescription.setText(teaProfileDescription.replaceAll("<br />",""));
            imgimage = (ImageView)rootView.findViewById(R.id.imgimage);

            final ProgressBar progressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);

            ImageLoader.getInstance().displayImage(teaProfileImage, imgimage, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    progressBar.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    progressBar.setVisibility(View.GONE);
                }
            });

        }

        else {

            build.setMessage("This application requires Internet connection. Would you connect to internet ?");
            build.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

                }
            });
            build.setNegativeButton("No", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    getActivity().finish();
                }
            });
            dialog2 = build.create();
            dialog2.show();

        }


        btncart = (Button) rootView.findViewById(R.id.btncart);
        btncart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int teaProfileId = getArguments().getInt("teaProfileId");
                String teaProfileName = getArguments().getString("teaProfileName");
                Float teaProfilePrice = getArguments().getFloat("teaProfilePrice");
                String teaProfileImage = getArguments().getString("teaProfileImage");
                String teaProfileLabel = getArguments().getString("teaProfileLabel");
                int teaProfilePieces = getArguments().getInt("teaProfilePieces");
                String teaProfilePiecesType = getArguments().getString("teaProfilePiecesType");
                String teaProfileDescription = getArguments().getString("teaProfileDescription");

                Toast.makeText(getActivity(), teaProfileName+" was added to cart.", Toast.LENGTH_LONG).show();

                ProductModel productsObject = ct.getProducts(teaProfileId);

                    ct.getCart().setProducts(productsObject);

            }

        });

        btnview = (Button) rootView.findViewById(R.id.btnview);
        btnview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent in = new Intent(getActivity(),Screen1.class);
                startActivity(in);
            }
        });
        return rootView;
    }
}
public class Controller extends Application {

    private ArrayList<ProductModel> myproducts = new ArrayList<ProductModel>();

    private ModelCart myCart = new ModelCart();

    public ProductModel getProducts(int id){

        return myproducts.get(id);

    }

    public void setProducts(ProductModel products){

        myproducts.add(products);

    }

    public ModelCart getCart(){

        return myCart;

    }

}
public class ModelCart {

    private ArrayList<ProductModel> cartItems = new ArrayList<ProductModel>();

    public ProductModel getProducts(int position){

        return cartItems.get(position);


    }

    public void setProducts(ProductModel Products){

        cartItems.add(Products);

    }


    public int getCartsize(){

        return cartItems.size();

    }

    public boolean CheckProductInCart(ProductModel aproduct){


        return cartItems.contains(aproduct);


    }

}
这是第19行:返回myproducts.get(id);
这是第192行:ProductModel productsObject=ct.getProducts(teaProfileId)

简而言之,您正在尝试访问包含0个元素的内容的第52个元素。日志/堆栈可能会指示发生这种情况的位置。因此,包含这一点很可能会有帮助。您的异常必须具有堆栈跟踪,在logcat@MikeTalready中发布italready post错误在logcat@ramanlfc中发布错误我认为问题在于:
ProductModel productsObject=ct.getProducts(teaProfileId)
teaProfileId
的值是多少
    java.lang.IndexOutOfBoundsException: Invalid index 51, size is 0
                                                         at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)

.Controller.getProducts(Controller.java:19)

.ProductDetails$4.onClick(ProductDetails.java:192)