gpt4 book ai didi

java - 转换应用程序上下文会出现不可转换的类型错误

转载 作者:行者123 更新时间:2023-12-01 18:26:29 25 4
gpt4 key购买 nike

所以我正在构建一个购物车应用程序,因为我正在学习 Android 开发,但是一行代码不断地给我一个错误。

具体就是这一行final Controller aController = (Controller) getApplicationContext();

我是 Java 和 Android 开发新手,我尝试过的所有方法都不起作用,所以我正在寻求帮助。

这是我在 MainActivity、Controller 类本身和 Manifest 文件中的代码。

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
final Button secondBtn = (Button) findViewById(R.id.second);

//Get Global Controller Class object (see application tag in AndroidManifest.xml)
// final Controller aController = new Controller();
final Controller aController = (Controller) getApplicationContext(); // Line giving me problems.


/****************** Create Dummy Products Data ***********/

Products productObject = null;
for(int i=1;i<=4;i++)
{
int price = 10+i;
// Create product model class object
productObject = new Products("Product "+i,"Description "+i,price);

//store product object to array list in controller
aController.setProducts(productObject);

}

/****************** Products Data Creation End ***********/


/******* Create view elements dynamically and show on activity ******/

//Product array list size
int ProductsSize = aController.getProductsArraylistSize();

// create the layout params that will be used to define how your
// button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);

/******** Dynamically create view elements - Start **********/

for(int j=0;j< ProductsSize;j++)
{
// Get product data from product data array list
String pName = aController.getProducts(j).getProductName();
int pPrice = aController.getProducts(j).getProductPrice();

// Create LinearLayout to view elemnts
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);

TextView product = new TextView(this);
product.setText(" "+pName+" ");

//Add textView to LinearLayout
ll.addView(product);

TextView price = new TextView(this);
price.setText(" $"+pPrice+" ");

//Add textView to LinearLayout
ll.addView(price);

final Button btn = new Button(this);
btn.setId(j+1);
btn.setText("Add To Cart");

// set the layoutParams on the button
btn.setLayoutParams(params);

final int index = j;

//Create click listener for dynamically created button
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

//Clicked button index
Log.i("TAG", "index :" + index);

// Get product instance for index
Products tempProductObject = aController.getProducts(index);

//Check Product already exist in Cart or Not
if(!aController.getCart().checkProductInCart(tempProductObject))
{
btn.setText("Added");

// Product not Exist in cart so add product to
// Cart product arraylist
aController.getCart().setProducts(tempProductObject);

Toast.makeText(getApplicationContext(),
"Now Cart size: " + aController.getCart().getCartSize(),
Toast.LENGTH_LONG).show();
}
else
{
// Cart product arraylist contains Product
Toast.makeText(getApplicationContext(),
"Product "+(index+1)+" already added in cart.",
Toast.LENGTH_LONG).show();
}
}
});

//Add button to LinearLayout
ll.addView(btn);

//Add LinearLayout to XML layout
lm.addView(ll);
}

/******** Dynamically create view elements - End **********/

secondBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

Intent i = new Intent(getBaseContext(), SecondActivity.class);
startActivity(i);
}
});
}
}

Controller.java

public class Controller {

private ArrayList<Products> myProducts = new ArrayList<Products>();
private Cart myCart = new Cart();


public Products getProducts(int pPosition) {

return myProducts.get(pPosition);
}

public void setProducts(Products Products) {

myProducts.add(Products);

}

public Cart getCart() {

return myCart;

}

public int getProductsArraylistSize() {

return myProducts.size();
}
}

AndroidManifest.xml

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.appdevy.projectestimation.Controller">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

最佳答案

您的 Controller 类需要扩展 android.app.Application

关于java - 转换应用程序上下文会出现不可转换的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25979914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com