Button 中的 Button (class) 无法应用于 class
Button (class) in Button cannot be applied to class
我正在编写一个使用 AsyncTask 的 Android 应用程序。我在 "onPostExecute" void 中收到以下错误:
Button (android.content.Context) in Button cannot be applied to >(com.****.OnlineProductList)
我已经尝试将 "this" 语句更改为“getContent()”和“MainAtivity.this ”。
当我输入 "get Content()" 时,它说:无法解析
当我输入 "MainActivity.this" 时,它说: 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]);
}
}
}
错误发生在这一行:
MainActivity.productButton[i] = new Button(this);
您需要传递一个 Context
来创建 Button
,目前您传递的是 OnlineProductList
(AsyncTask
) 对象。
您可以将 Context
实例作为构造函数参数(并保留 WeakReference
以避免内存泄漏)传递给 OnlineProductList
并使用 Context
来创建你的 Button
.
以下几行内容应该有效,
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]);
}
}
}
}
我正在编写一个使用 AsyncTask 的 Android 应用程序。我在 "onPostExecute" void 中收到以下错误:
Button (android.content.Context) in Button cannot be applied to >(com.****.OnlineProductList)
我已经尝试将 "this" 语句更改为“getContent()”和“MainAtivity.this ”。 当我输入 "get Content()" 时,它说:无法解析 当我输入 "MainActivity.this" 时,它说: 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]);
}
}
}
错误发生在这一行:
MainActivity.productButton[i] = new Button(this);
您需要传递一个 Context
来创建 Button
,目前您传递的是 OnlineProductList
(AsyncTask
) 对象。
您可以将 Context
实例作为构造函数参数(并保留 WeakReference
以避免内存泄漏)传递给 OnlineProductList
并使用 Context
来创建你的 Button
.
以下几行内容应该有效,
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]);
}
}
}
}