有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java订单按钮未绑定到firebase

我正在食品订单应用程序中创建购物车按钮和购物车。单击cart floatingaction按钮后出现错误,下面是日志cat和CartaAdapter。JAVA在logcat中,第72行和第89行出现错误。同时给出了预期输出的屏幕截图和错误输出。我在点击PlaceOrder按钮并给出addrress后再次出错。不幸的是,点击订单后停止。 error pic1 expected ouput after clicking yes on order pic2

Logcat错误

  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.sharma.digimenu.Model.User.getPhone()' on a null object reference
        at com.example.sharma.digimenu.Cart$2.onClick(Cart.java:95)
        at 安卓.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
        at 安卓.os.Handler.dispatchMessage(Handler.java:102)
        at 安卓.os.Looper.loop(Looper.java:135)
        at 安卓.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:699)

购物车。java

public class Cart extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

    FirebaseDatabase database;
    DatabaseReference requests;

    TextView txtTotalPrice;
    Button btnPlace;

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

    CartAdapter adapter;
    private DialogFragment dialogInterface;

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

        database = FirebaseDatabase.getInstance();
        requests = database.getReference("Requests");

        recyclerView = (RecyclerView) findViewById(R.id.listCart);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        txtTotalPrice = (TextView) findViewById(R.id.total);
        btnPlace = (Button) 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 editAddress = new EditText(Cart.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );
        editAddress.setLayoutParams(lp);
        alertDialog.setView(editAddress);
        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(),
                        editAddress.getText().toString(),
                        txtTotalPrice.getText().toString(),
                        cart
                );

                requests.child(String.valueOf(System.currentTimeMillis()))
                        .setValue(request);
                new Database(getBaseContext()).cleanCart();
                Toast.makeText(Cart.this,"Thankyou ,Order Placed",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) { 

                dialogInterface.dismiss();

            }
        });
        alertDialog.show();
    }
    private void loadListFood() {

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

        int total= 0;
        for (Order order:cart)
            total+=(Integer.parseInt(order.getPrice()))*(Integer.parseInt(order.getQuantity()));

        Locale locale = new Locale("en", "IN");
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);

        txtTotalPrice.setText(fmt.format(total));
    }
}

获取此公用项上的错误。当前用户。getPhone()

常见。java

  package com.example.sharma.digimenu.Common;

import com.example.sharma.digimenu.Model.Category;
import com.example.sharma.digimenu.Model.User;

public class Common
{
    public static User currentUser;
    public static Category currentuser;
}

请求。java

import java.util.List;

public class Request {

    private String phone;
    private String name;
    private String address;
    private String total;

    private List<Order>foods;

    public Request() {

    }

    public Request(String phone, String name, String address, String total, List<Order> foods) {
        this.phone = phone;
        this.name = name;
        this.address = address;
        this.total = total;
        this.foods = foods;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public List<Order> getFoods() {
        return foods;
    }

    public void setFoods(List<Order> foods) {
        this.foods = foods;
    }
}

用户。java

public class User {

    private String email, password, name, phone;

    public User(String email, String password, String name,String phone) {
        this.email = email;
        this.password = password;
        this.name = name;
    }

    public User() {

    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email){
        this.email = email;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String password){
        this.phone = phone;
    }
    public String getName() {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您正在返回一个CardViewHolder的实例,而您的ViewHolder名为CartViewHolder

        @NonNull
        @Override
        public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View itemView = inflater.inflate(R.layout.cart_layout, parent, false);
    
            return new CardViewHolder(itemView); <   - here's the problem
        }
    

    也可以将CardViewHolder更改为CartViewHolderCartAdapter类中的任何位置