Search code examples
javaandroidandroid-asynctaskandroid-buttonandroid-context

Button (class) in Button cannot be applied to class


I'm coding an Android app that uses an AsyncTask. I get the following error in the "onPostExecute" void :

Button (android.content.Context) in Button cannot be applied to >(com.****.OnlineProductList)

I already tried to change the "this" statement to "getContent()" and "MainAtivity.this". When I type in "get Content()" it says: cannot resolve When I type in "MainActivity.this" it says: MainActivity is not an enclosing class


    public class OnlineProductList extends AsyncTask<Void,Void,Void>{


        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);


            System.out.println("Program: " + productData.length / 3 + " Products found");

//create product objects



            Product[] product = new Product[productData.length / 3];
            for (int i = 0; i < product.length; i++) {

                product[i] = new Product(productData[3 * i], productData[1 + 3 * i], Boolean.parseBoolean(productData[2 + 3 * i]));

            }

            //create buttons for available products

            MainActivity.productButton = new Button[product.length];

            for (int i = 0; i < productData.length; i++) {

                MainActivity.productButton[i] = new Button(this);
                MainActivity.productButton[i].setWidth(MainActivity.orderHorizontalLayout.getWidth());
                MainActivity.productButton[i].setHeight(MainActivity.orderHorizontalLayout.getHeight() / 10);
                MainActivity.productButton[i].setText(product[i].name);

                MainActivity.orderHorizontalLayout.addView(MainActivity.productButton[i]);

            }
        }
    }

The error occurs in this line:

MainActivity.productButton[i] = new Button(this);

Solution

  • You need to pass a Context for the creation of Button, currently you're passing the OnlineProductList(AsyncTask) object.

    You could just pass a Context instance as a constructor parameter (and keep a WeakReference to it to avoid memory leaks) to the OnlineProductList and use that Context to create your Button.

    Something along the following lines should work,

    
        public class OnlineProductList extends AsyncTask<Void,Void,Void>{
            private WeakReference<Context> contextRef;
    
            public OnlineProductList(Context context) {
                contextRef = new WeakReference<>(context);
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
    
    
                System.out.println("Program: " + productData.length / 3 + " Products found");
    
    //create product objects
    
    
    
                Product[] product = new Product[productData.length / 3];
                for (int i = 0; i < product.length; i++) {
    
                    product[i] = new Product(productData[3 * i], productData[1 + 3 * i], Boolean.parseBoolean(productData[2 + 3 * i]));
    
                }
    
                //create buttons for available products
    
                Context context = contextRef.get();
                if (context != null) {
                    MainActivity.productButton = new Button[product.length];
    
                    for (int i = 0; i < productData.length; i++) {
    
                        MainActivity.productButton[i] = new Button(this);
                        MainActivity.productButton[i].setWidth(MainActivity.orderHorizontalLayout.getWidth());
                        MainActivity.productButton[i].setHeight(MainActivity.orderHorizontalLayout.getHeight() / 10);
                        MainActivity.productButton[i].setText(product[i].name);
    
                        MainActivity.orderHorizontalLayout.addView(MainActivity.productButton[i]);
                    }
                }
            }
        }