Ich mache eine E-Commerce-Anwendung und ich bin für die App hier ist mein CodeErste java.lang.ClassCastException in meiner app
Controller.java
package com.example.android.namkeen;
import android.app.Application;
import java.util.ArrayList;
/**
* Created by SONY on 02-10-2016.
*/
public class Controller extends Application
{
private ArrayList<ModelProducts> myproducts = new ArrayList<ModelProducts>();
private ModelCart myCart = new ModelCart();
public ModelProducts getProducts(int pPosition){
return myproducts.get(pPosition);
}
public void setProducts(ModelProducts products){
myproducts.add(products);
}
public ModelCart getCart(){
return myCart;
}
public int getProductArraylistsize(){
return myproducts.size();
}
}
ModelCart.java machen Wagen
package com.example.android.namkeen;
import java.util.ArrayList;
public class ModelCart {
private ArrayList<ModelProducts> cartItems = new ArrayList<ModelProducts>();
public ModelProducts getProducts(int position){
return cartItems.get(position);
}
public void setProducts(ModelProducts Products){
cartItems.add(Products);
}
public int getCartsize(){
return cartItems.size();
}
public boolean CheckProductInCart(ModelProducts aproduct){
return cartItems.contains(aproduct);
}
}
ModelProducts.java
package com.example.android.namkeen;
/**
* Created by SONY on 02-10-2016.
*/
public class ModelProducts {
private String productName;
private String productDesc;
private int productPrice;
public ModelProducts(String productName,String productDesc,int productPrice){
this.productName = productName;
this.productDesc = productDesc;
this.productPrice = productPrice;
}
public String getProductName(){
return productName;
}
public String getProductDesc(){
return productDesc;
}
public int getProductPrice(){
return productPrice;
}
}
und dies, in dem die Haupttätigkeit ist ich eine benutzerdefinierte Ansicht mache, und ich bin mit Controller-Klasse, die Produkte
package com.example.android.namkeen;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class CartMain extends AppCompatActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout layout = (LinearLayout)findViewById(R.id.linearMain);
final Button btn = (Button)findViewById(R.id.second);
final Controller ct = (Controller) getApplicationContext();//getting error in this line
ModelProducts products = null;
int Price=30;
products = new ModelProducts("Plain Maath", "Plain salted \n big sized mathri", Price);
ct.setProducts(products);
/* for(int i= 1; i<=7;i++)
{
int Price = 15+ i;
products = new ModelProducts("Product Item" +i, "Description"+i, Price);
ct.setProducts(products);
}*/
int productsize = ct.getProductArraylistsize();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
for (int j=0;j< productsize;j++){
String pName = ct.getProducts(j).getProductName();
int pPrice = ct.getProducts(j).getProductPrice();
String desc = ct.getProducts(j).getProductDesc();
LinearLayout la = new LinearLayout(this);
la.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout la1 = new LinearLayout(this);
la1.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText(" " + pName + " ");
tv.setTypeface(null, Typeface.BOLD);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22F);
la1.addView(tv);
TextView des = new TextView(this);
des.setText(" " + desc + " ");
la1.addView(des);
TextView tv1 = new TextView(this);
tv1.setText(" "+"Rs"+pPrice+"/250gm"+" ");
la1.addView(tv1);
la.addView(la1);
final Button btn1 = new Button(this);
btn1.setId(j+1);
btn1.setText("Add to Cart");
btn1.setLayoutParams(params);
final int index = j;
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("TAG", "index:"+index);
ModelProducts productsObject = ct.getProducts(index);
if(!ct.getCart().CheckProductInCart(productsObject)){
btn1.setText("Item Added");
ct.getCart().setProducts(productsObject);
Toast.makeText(getApplicationContext(), "New CartSize:" +ct.getCart().getCartsize(),Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Products"+(index+1)+"Already Added",Toast.LENGTH_LONG).show();
}
}
});
la.addView(btn1);
layout.addView(la);
}
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getBaseContext(),Screen2.class);
startActivity(in);
}
});
}
}
Dies ist der Code hinzuzufügen, die beim Check-out-Taste in der Warenkorb gelegt Aktivität geklickt wird funktionieren Screen2.java
package com.example.android.namkeen;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
/**
* Created by SONY on 02-10-2016.
*/
public class Screen2 extends AppCompatActivity
{
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle) */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
TextView showCartContent = (TextView)findViewById(R.id.showcart);
final Controller ct = (Controller)getApplicationContext();
final int CartSize = ct.getCart().getCartsize();
String show = "";
for(int i=0;i<CartSize;i++){
String pName = ct.getCart().getProducts(i).getProductName();
int pPrice = ct.getCart().getProducts(i).getProductPrice();
String pDisc = ct.getCart().getProducts(i).getProductDesc();
show += "Product Name:"+pName+" "+"Price : "+pPrice+""+"Discription : "+pDisc+""+ "-----------------------------------";
}
showCartContent.setText(show);
}
}
ich alles tue, richtig, aber wenn ich diesen Code leite es gebe diese Ausnahmefehler
java.lang.RuntimeException: Kann Aktivität ComponentInfo starten {com.example.an droid.namkeen/com.example.android.namkeen.CartMain}: java.lang.ClassCastException: android.app.Application kann nicht in com.example.android.namkeen.Controller
Verursacht werden von: java.lang. Classcast: android.app.Application kann nicht auf com.example.android.namkeen.Controller gegossen werden bei com.example.android.namkeen.CartMain.onCreate (CartMain.java:32)
ich bin nicht in der Lage zu verstehen, was sollte ich tun, wo ich meinen Code ändern sollte bitte helfen, danke im voraus
Ich habe Ihre Lösung versucht, aber keine Verwendung gibt es den gleichen Fehler und ich habe es auch in Manifest-Datei – Vikas
Pls zeigen mir den relevanten Teil des Manifests, wo Sie den Controller als Ihre Anwendung Klasse deklarieren. Verwenden Sie Module? – Alex
Ich habe dein Singleton-Konzept sehr gerne benutzt – Vikas