简体   繁体   中英

When i click on the button (add order to cart) it work but when i try to open the cart, Android Studio gives an error

I believe the problem is either with the FButton or recycle view,not sure if i did everything right especially with the CartViewHolder,,,,, am kinda confused today, i have been fixing problems this app have been giving me all day long but this one seems to be something i will never see. please help.. i need to figure this out to move on.

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.reds0n.foodorderclient, PID: 7212
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.reds0n.foodorderclient/com.example.reds0n.foodorderclient.Cart}: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721)
                  at android.app.ActivityThread.access$900(ActivityThread.java:168)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5753)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
               Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
                  at android.view.LayoutInflater.createView(LayoutInflater.java:633)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                  at com.example.reds0n.foodorderclient.Cart.onCreate(Cart.java:51)
                  at android.app.Activity.performCreate(Activity.java:6112)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:168) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5753) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Constructor.newInstance(Native Method)
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:607)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                  at com.example.reds0n.foodorderclient.Cart.onCreate(Cart.java:51) 
                  at android.app.Activity.performCreate(Activity.java:6112) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:168) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5753) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
               Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x5
                  at android.content.res.TypedArray.getColor(TypedArray.java:405)
                  at info.hoang8f.widget.FButton.parseAttrs(FButton.java:116)
                  at info.hoang8f.widget.FButton.<init>(FButton.java:52)
                  at java.lang.reflect.Constructor.newInstance(Native Method) 
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
                  at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                  at com.example.reds0n.foodorderclient.Cart.onCreate(Cart.java:51) 
                  at android.app.Activity.performCreate(Activity.java:6112) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:168) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5753) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

I/Process: Sending signal. PID: 7212 SIG: 9 Application terminated.here

This is the cart_layout.xml file, Displays the orders name, price and has an image.. Its also used with the CartAdapter class.

 <?xml version="1.0" encoding="utf-8"?>
     <android.support.v7.widget.CardView
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:cardElevation="5dp"
     android:layout_marginTop="8dp">



   <LinearLayout
       android:layout_marginTop="16dp"
       android:layout_marginBottom="16dp"
       android:orientation="horizontal"
       android:gravity="center_vertical"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">


       <LinearLayout
           android:orientation="vertical"
           android:layout_weight="9"
           android:layout_width="0dp"
           android:layout_height="wrap_content">

         <TextView
              android:id="@+id/item_cart_name"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="center_vertical|start"
              android:text="food"
              android:textAllCaps="true"
              android:textColor="@color/fbutton_color_belize_hole"
              android:textStyle="italic" />

          <TextView
              android:id="@+id/item_cart_price"
              android:gravity="center_vertical|start"
              android:text="$100"
              android:textColor="@color/red"
              android:textStyle="bold"
              android:textAllCaps="true"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />

          <ImageView
            android:id="@+id/item_cart_count"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center_vertical|end"/>

      </LinearLayout>


  </LinearLayout>

public class Cart extends AppCompatActivity {

private RecyclerView recyclerView;
private TextView totalPrice;
private DatabaseReference mDatabase;


private FButton placeOrder;

List<orders> cart = new ArrayList<>();

CartAdapter adapter;

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



    recyclerView = (RecyclerView) findViewById(R.id.recycle_food);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    totalPrice = (TextView) findViewById(R.id.Total);
    placeOrder = (FButton) findViewById(R.id.cartbtn);


    placeOrder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });



    loadOrders();

}







private void loadOrders() {

    cart =new Database(this).getCarts();
    adapter = new CartAdapter(cart,this);
    recyclerView.setAdapter(adapter);


    ///calculations

    int total = 0;
    for(orders Orders: cart){
        total += Integer.parseInt(Orders.getPrice())*Integer.parseInt(Orders.getQuantity());


        Locale locale = new Locale("en","US");
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        totalPrice.setText(fmt.format(total));
    }


}

public static class CartViewHolder extends RecyclerView.ViewHolder {

    private TextView cart_name,cart_price;
    private ImageView cartimage;

    View mView;
    public  CartViewHolder(View itemView){
        super(itemView);
        mView=itemView;

        cart_name = (TextView) mView.findViewById(R.id.item_cart_name);
        cart_price = (TextView)  mView.findViewById(R.id.item_cart_price);
        cartimage = (ImageView)  mView.findViewById(R.id.item_cart_count) ;

    }

    public void setName(String name){
        TextView food_name = (TextView) mView.findViewById(R.id.item_cart_name);
        food_name.setText(name);


    }


    public void setPrice(String price){
        TextView food_name = (TextView) mView.findViewById(R.id.item_cart_price);
        food_name.setText(price);


    }




    public void setImage(Context ctx, String image ){

        ImageView food_image = (ImageView) mView.findViewById(R.id.item_cart_count);
        Picasso.with(ctx).load(image).into(food_image);



    }


}



public class CartAdapter extends RecyclerView.Adapter<CartViewHolder>{
    private  List<orders> listData = new ArrayList<>();
    private Context context;


    public CartAdapter( List<orders> listData, Context context) {
        this.context = context;
        this.listData = listData;
    }

    @Override
    public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View mView  = inflater.inflate(R.layout.cart_layout,parent,false);

        return new CartViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(CartViewHolder holder, int position) {

        TextDrawable drawable = TextDrawable.builder().buildRound(""+ listData.get(position).getQuantity(), Color.WHITE);
        holder.cartimage.setImageDrawable(drawable);

        Locale locale = new Locale("en","US");
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        int price = (Integer.parseInt(listData.get(position).getPrice()))*(Integer.parseInt(listData.get(position).getQuantity()));
        holder.cart_price.setText(fmt.format(price));

        holder.cart_name.setText(listData.get(position).getProductname());

    }

    @Override
    public int getItemCount() {
        return listData.size();
    }
}

}

This is the activity_cart.xml, its has cardview at the bottom to display the price and also a button to place order,

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:background="@drawable/backgground"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.reds0n.foodorderclient.Cart">


    <android.support.v7.widget.RecyclerView
    android:id="@+id/cartList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fbutton_color_transparent"/>


<android.support.v7.widget.CardView
    android:layout_alignParentBottom="true"
    app:cardBackgroundColor="@color/fbutton_color_pumpkin"
    android:layout_width="match_parent"
    android:layout_height="100dp">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
    android:layout_margin="8dp"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView

    android:text="Total: "
    android:textSize="20sp"
    android:textColor="@color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/Total"
            android:text="$200 "
            android:textSize="24sp"
            android:textStyle="italic"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

  </LinearLayout>
    <info.hoang8f.widget.FButton
        android:id="@+id/cartbtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:height="10dp"
        app:buttonColor="@color/tryme"
        app:shadowColor="@color/white"
        android:text="Place Order"
        android:textStyle="bold"
        app:cornerRadius="10dp"
        app:shadowEnabled="true"
        android:textColor="@color/white"/>
</RelativeLayout>
</android.support.v7.widget.CardView>


</RelativeLayout>

The FButton was the issue.. I removed the FButton in activity_cart and replaced it with Button, it worked fine. Dont know why it did tho Thanks..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM