当我按下购物车按钮时,我的应用程序崩溃了

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当我按下购物车按钮时,我的应用程序崩溃了相关的知识,希望对你有一定的参考价值。

当我按下购物车按钮时,我的应用程序崩溃。以下是我的android应用程序的购物车代码。

public class Cart extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;

FirebaseDatabase database;
DatabaseReference requests;
TextView txtTotalPrice;
FButton btnPlace;
List<Order> cart=new ArrayList<>();
CartAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
    //Firebase
    database = FirebaseDatabase.getInstance();
    requests = database.getReference("Requests");
    //Init
    recyclerView = (RecyclerView) findViewById(R.id.listCart);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    txtTotalPrice = (TextView) findViewById(R.id.total);
    btnPlace =(FButton)findViewById(R.id.btnPlaceOrder);
    btnPlace.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
           showAlertDialog();

        }
    });

    loadlistfood();


}

private void showAlertDialog() {

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
    alertDialog.setTitle("One more step!");
    alertDialog.setMessage("Enter your Address: ");
    final EditText edtAddress = new EditText(Cart.this);
   LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
    );

    edtAddress.setLayoutParams(lp);
    alertDialog.setView(edtAddress);//add edt text to alert box
    alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Request request = new Request(
                    Common.currentUser.getPhone(),
                    Common.currentUser.getName(),
                    edtAddress.getText().toString(),
                    txtTotalPrice.getText().toString(),
                    cart
            );
            //submit to firebase
            //we will use system.currntMill to key
            requests.child(String.valueOf(System.currentTimeMillis()))
                    .setValue(request);
            //Delete cart
            new Database(getBaseContext()).cleanCart();
            Toast.makeText(Cart.this, "Thank you,Order placed", Toast.LENGTH_SHORT).show();
            finish();

        }
    });
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    alertDialog.show();
}

private void loadlistfood() {
    cart=new Database(this).getCarts();
    adapter=new CartAdapter(cart,this);
    recyclerView.setAdapter(adapter);

    //calculate price
    int total=0;
    for(Order order:cart)
        total+=(Integer.parseInt(order.getPrice()))* 
(Integer.parseInt(order.getQuantity()));
    Locale locale=new Locale("en","US");
    NumberFormat fmt=NumberFormat.getCurrencyInstance(locale);
    txtTotalPrice.setText(fmt.format(total));


}
}

这是调试报告

> 05-02 21:27:21.970 7698-7698/com.example.admin.clicknorder
> E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache 05-02
> 21:27:21.970 7698-7698/com.example.admin.clicknorder E/OpenGLRenderer:
> MAX_TEXTURE_SIZE: 16384 05-02 21:27:21.970
> 7698-7698/com.example.admin.clicknorder E/OpenGLRenderer: Getting
> MAX_TEXTURE_SIZE from Caches::initConstraints() 05-02 21:27:21.970
> 7698-7698/com.example.admin.clicknorder E/OpenGLRenderer:
> MAX_TEXTURE_SIZE: 16384 05-02 21:27:49.030
> 7698-7698/com.example.admin.clicknorder E/dalvikvm: Could not find
> class 'android.graphics.drawable.RippleDrawable', referenced from
> method
> android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
> 05-02 21:28:02.138 7698-7698/com.example.admin.clicknorder
> E/AndroidRuntime: FATAL EXCEPTION: main
>                                                                              Process: com.example.admin.clicknorder, PID: 7698
>                                                                              java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.admin.clicknorder/com.example.admin.clicknorder.Cart}:
> android.view.InflateException: Binary XML file line #11: Error
> inflating class android.support.v7.app.AlertController.RecycleListView
>                                                                                  at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
>                                                                                  at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
>                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:135)
>                                                                                  at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
>                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
>                                                                                  at android.os.Looper.loop(Looper.java:136)
>                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001)
>                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
>                                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
>                                                                                  at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
>                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
>                                                                                  at dalvik.system.NativeStart.main(Native Method)
>                                                                               Caused by: android.view.InflateException: Binary XML file line #11:
> Error inflating class
> android.support.v7.app.AlertController.RecycleListView
>                                                                                  at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
>                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
>                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
>                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
>                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
>                                                                                  at
> android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
>                                                                                  at
> android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
>                                                                                  at com.example.admin.clicknorder.Cart.onCreate(Cart.java:46)
>                                                                                  at android.app.Activity.performCreate(Activity.java:5231)
>                                                                                  at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
>                                                                                  at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
>                                                                                  at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
>                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
>                                                                                  at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
>                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
>                                                                                  at android.os.Looper.loop(Looper.java:136) 
>                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001) 
>                                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
>                                                                                  at java.lang.reflect.Method.invoke(Method.java:515) 
>                                                                                  at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
>                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
>                                                                                  at dalvik.system.NativeStart.main(Native Method) 
>                                                                               Caused by: java.lang.ClassNotFoundException: Didn't find class
> "android.support.v7.app.AlertController.RecycleListView" on path:
> DexPathList[[zip file
> "/data/app/com.example.admin.clicknorder-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.admin.clicknorder-2,
> /system/lib]]
>                                                                                  at
> dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
>                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
>                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
>                                                                                  at android.view.LayoutInflater.createView(LayoutInflater.java:559)
>                                                                                  at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
>                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
>                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
>                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
>                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
>                                                                                  at
> android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
>                                                                                  at
> android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
>                                                                                  at com.example.admin.clicknorder.Cart.onCreate(Cart.java:46) 
>                                                                                  at android.app.Activity.performCreate(Activity.java:5231) 
>                                                                                  at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
>                                                                                  at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
>                                                                                  at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
>                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
>                                                                                  at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
>                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
>                                                                                  at android.os.Looper.loop(Looper.java:136) 
>                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001) 
>                                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
>                                                                                  at java.lang.reflect.Method.invoke(Method.java:515) 
>                                                                                  at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
>                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
>                                                                                  at dalvik.system.NativeStart.main(Native Method)

最后这里是购物车活动xml代码

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@drawable/background"
tools:context="com.example.admin.clicknorder.Cart">

<android.support.v7.app.AlertController.RecycleListView
    android:id="@+id/listCart"
    android:background="@android:color/transparent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
<android.support.v7.widget.CardView
    android:layout_alignParentBottom="true"
    app:cardBackgroundColor="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="100dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:orientation="horizontal"
            android:layout_margin="8dp"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
            android:text="Total:"
            android:textSize="20sp"
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/total"
                android:text="Rs 1000"
                android:textSize="20sp"
                android:textColor="@android:color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <info.hoang8f.widget.FButton
            android:id="@+id/btnPlaceOrder"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:text="Place Order"
            android:textColor="@android:color/holo_red_dark"
            app:buttonColor="@color/btnSignActive"
            app:cornerRadius="4dp"
            app:shadowColor="@android:color/black"
            app:shadowEnabled="true"
            android:layout_marginTop="40dp"
            android:layout_below="@+id/linearLayout2"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

答案

在您使用的布局中

<android.support.v7.app.AlertController.RecycleListView

但是在您使用的代码中

RecyclerView

两个都不同我的朋友。

另一答案

RecycleListView是一个内部类,不可供外部使用。

更改

android.support.v7.app.AlertController.RecycleListView

android.support.v7.widget.RecyclerView

RecyclerView

以上是关于当我按下购物车按钮时,我的应用程序崩溃了的主要内容,如果未能解决你的问题,请参考以下文章

当我按下一个按钮时,“不幸的是我的应用程序已停止”[重复]

当我按下一个按钮时我的游戏崩溃了(以 NSException 类型的未捕获异常终止)

我有一个带有许多 textEdits 的片段,当我按下按钮时,这些字符串应该放在表格列中[重复]

使用片段清除回栈

应用程序在主要活动中关闭onBackPressed

当Webview方向更改然后按下后退按钮时,应用程序崩溃