Android - Firebase数据库异常

时间:2017-09-01 18:05:56

标签: android firebase

我从firebase获得异常“com.google.firebase.database.DatabaseException:无法将java.lang.Long类型的对象转换为类型”单击片段1上的按钮时我收到错误并且我切换我的应用程序中的另一个片段(另一个选项卡)。我不明白为什么。下面的代码显示了两个片段。

片段1:

 depositButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            depositInteger = 0;
            try {
                depositInteger = 
       Integer.parseInt(depositText.getText().toString().trim());
            } catch (NumberFormatException e) {
                if (depositText.equals("")) {
                    Toast.makeText(getActivity(), "Failed", 
                    Toast.LENGTH_SHORT).show();
                }
            }
            DatabaseReference rootRef = 
            FirebaseDatabase.getInstance().getReference();
            final DatabaseReference userRef = 
            rootRef.child("Users").child(user.getDisplayName());


            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(!dataSnapshot.hasChild("deposit")){
                        userRef.child("deposit").setValue(0);
                    } else {
                        previousDeposit = 
           dataSnapshot.child("deposit").getValue(Integer.class);
                        finalDeposit = previousDeposit + depositInteger;

                        userRef.child("deposit").setValue(finalDeposit);
                        deposit.setText(String.valueOf(finalDeposit));

                    }


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {}
            };
            userRef.addListenerForSingleValueEvent(eventListener);

        }
    });

片段2:

 database.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    int oddsText = 0;
                    int betAmount = 0;
                    try {

                        oddsText = 
                 Integer.parseInt(mOddsText.getText().toString().trim());
                        betAmount = 
                 Integer.parseInt(mBetAmount.getText().toString().trim());

                    } catch (NumberFormatException e) {
                        if (mOddsText.equals("")) {
                            Toast.makeText(getActivity(), "Failed", 
                           Toast.LENGTH_SHORT).show();
                        } else if (mBetAmount.equals("")) {
                            Toast.makeText(getActivity(), "Failed", 
                            Toast.LENGTH_SHORT).show();
                        }
                    }

                    if (oddsText > 0 & betAmount > 0) {
                        String id = database.push().getKey();
                        database.child(user.getDisplayName()).child(id).child("odds").setValue(oddsText);
                        database.child(user.getDisplayName()).child(id).child("betAmount").setValue(betAmount);
                        database.child(user.getDisplayName()).child(id).child("date").setValue(mDate);
                        database.child(user.getDisplayName()).child(id).child("result").setValue(result);

                        Toast.makeText(getActivity(), "Done", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

1 个答案:

答案 0 :(得分:1)

您正在从数据库中检索一个数字,该数字以long形式返回并直接将其转换为int给出错误

试试这个

int number= (int) (long) dataSnapshot.getvalue()

这是问题

previousDeposit = `dataSnapshot.child("deposit").getValue(Integer.class);`

将其转换为

previousDeposit =(int) (long) dataSnapshot.child("deposit").getValue();