我认为这个问题要么是FButton还是回收视图,不确定我是否做得对,特别是CartViewHolder ,,,,今天有点困惑,我一直在解决这个应用程序给我一整天的问题很久但这个似乎是我永远不会看到的东西。请帮助..我需要弄清楚这一点继续前进。
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 /处理:发送信号。 PID:7212 SIG:9 申请已终止。
这是cart_layout.xml文件,显示订单名称,价格并有图像..它也与CartAdapter类一起使用。
<?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();
}
}
}
这是activity_cart.xml,它的底部有cardview以显示价格,还有一个按钮来下订单,
<?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>
答案 0 :(得分:0)
FButton是个问题.. 我在activity_cart中删除了FButton并将其替换为Button,它运行正常。 不知道为什么会这样做 感谢..