2016-11-02 34 views
0

我在做一个电子商务应用程序,我正在车在这里的应用程序是我的代码获得java.lang.ClassCastException在我的应用程序

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

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; 
    } 
} 

,这是主要的活动中,我想提出一个自定义视图,我用控制器类添加的产品

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); 
       } 
      }); 
     } 
    } 

这是当结账按钮,将在车活动 被点击,将工作代码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); 
    } 
} 

我做的一切正确的,但,当我运行这段代码是给这个异常错误

了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.an droid.namkeen/com.example.android.namkeen.CartMain}:java.lang.ClassCastException:android.app.Application不能转换为com.example.android.namkeen.Controller

引起来自:java.lang。 ClassCastException异常:android.app.Application不能com.example.android.namkeen.CartMain.onCreate(CartMain.java:32)

被转换为com.example.android.namkeen.Controller 我无法理解我应该怎么做,我应该改变我的代码 请帮助提前致谢

回答

1

当您想要检索Ap时,应该使用getApplication()而不是getApplicationContext()折衷实例。

也希望您在清单文件中将Controller类声明为Application类。

此外,将您的Application类继承以存储其中的数据并不是最好的想法...您应该改为使用单例类。

+0

我已经尝试过您的解决方案,但没有使用它给出了相同的错误,我也在清单文件中声明了它 – Vikas

+0

请向我展示声明Controller作为Application类的清单的相关部分。你在使用模块吗? – Alex

+0

我已经使用你的单身概念非常感谢 – Vikas

相关问题