簡體   English   中英

從 Firebase 在 Android 中獲取數據時出錯

[英]Error while Fetching Data in Android from Firebase

這是我過去兩天一直面臨的錯誤。

請給我同樣的解決方案

進程:com.example.umangthakkar.electrofycustomer,PID:25756 com.google.firebase.database.DatabaseException:無法將 java.lang.String 類型的對象轉換為 com.example.umangthakkar.electrofycustomer.BillPaid

進程:com.example.umangthakkar.electrofycustomer,PID:25756
com.google.firebase.database.DatabaseException:無法將 java.lang.String 類型的對象轉換為 com.example.umangthakkar.electrofycustomer.BillPaid 在 com.google.android.gms.internal.zg.zzb(未知來源) )
在 com.google.android.gms.internal.zg.zza(來源不明)
在 com.google.firebase.database.DataSnapshot.getValue(來源不明)
在 com.example.umangthakkar.electrofycustomer.RecyclerView_MyBills$1.onDataChange(RecyclerView_MyBills.java:75)
在 com.google.android.gms.internal.to.zza(來源不明)
在 com.google.android.gms.internal.vj.zzHX(來源不明)
在 com.google.android.gms.internal.vp.run(來源不明)
在 android.os.Handler.handleCallback(Handler.java:815)
在 android.os.Handler.dispatchMessage(Handler.java:104)
在 android.os.Looper.loop(Looper.java:207)
在 android.app.ActivityThread.main(ActivityThread.java:5740)
在 java.lang.reflect.Method.invoke(Native Method)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)

下面是寫的代碼,請檢查以理解

package com.example.umangthakkar.electrofycustomer;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class RecyclerView_MyBills extends AppCompatActivity {

DatabaseReference dbBillPaid;

ListView rvCustomers;

//List<Customer> customerList;
List<BillPaid> billPaidList;

private ProgressBar progressBar;
   private TextView txtplwait;

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

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    String userid = user.getUid();
    dbBillPaid = FirebaseDatabase.getInstance().getReference("BillPaid").child(userid);
    rvCustomers = (ListView) findViewById(R.id.rvCustomers);
    //  rvCustomers.setHasFixedSize(true);

    //customerList = new ArrayList<>();
    billPaidList = new ArrayList<>();



    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    txtplwait = (TextView) findViewById(R.id.txtpwait);

    progressBar.setVisibility(View.VISIBLE);
    txtplwait.setVisibility(View.VISIBLE);

}

@Override
protected void onStart() {
    super.onStart();

    dbBillPaid.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            billPaidList.clear();


            for(DataSnapshot customerSnapshot : dataSnapshot.getChildren()){
                // Customer customer = customerSnapshot.getValue(Customer.class);
                BillPaid billPaid =  customerSnapshot.getValue(BillPaid.class);

                billPaidList.add(billPaid);
            }

            MyBillList adapter = new MyBillList(RecyclerView_MyBills.this,billPaidList);
            rvCustomers.setAdapter(adapter);

            progressBar.setVisibility(View.GONE);

            txtplwait.setVisibility(View.GONE);

            if (dataSnapshot.getValue()==null)
            {

                showBillsDailog();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

private void showBillsDailog() {

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.nomybills, null);
    dialogBuilder.setView(dialogView);


    final Button buttonyes = (Button) dialogView.findViewById(R.id.btnyes);
    //    final Button buttonno = (Button) dialogView.findViewById(R.id.btnno);

    dialogBuilder.setTitle("No Bills");
    final AlertDialog b = dialogBuilder.create();
    b.show();


    buttonyes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(RecyclerView_MyBills.this, Drawer_MainPage.class);
            startActivity(intent);
            finish();
            b.dismiss();
        }
    });


}
}

MyBillList.java :

package com.example.umangthakkar.electrofycustomer;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Umang Thakkar on 14/04/2018.
 */

public class MyBillList extends ArrayAdapter<BillPaid> {

private Activity context;
private List<BillPaid> billPaidList;

public MyBillList(Activity context, List<BillPaid> billPaidList)
{
    super(context,R.layout.activity_card_view_mybills,billPaidList);
    this.context=context;
    this.billPaidList=billPaidList;
    // this.customerList=customerList;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();

    View ListViewItem = inflater.inflate(R.layout.activity_card_view_mybills,null,true);

    //   TextView firstname= (TextView) ListViewItem.findViewById(R.id.fname);
    //   TextView lastname= (TextView) ListViewItem.findViewById(R.id.lname);
    //  TextView service= (TextView) ListViewItem.findViewById(R.id.service_no);
    TextView month= (TextView) ListViewItem.findViewById(R.id.month);
    TextView year= (TextView) ListViewItem.findViewById(R.id.year);
    TextView gross= (TextView) ListViewItem.findViewById(R.id.gross);

    //  Customer customer = customerList.get(position);

    BillPaid billPaid = billPaidList.get(position);

    //   firstname.setText(customer.getFname());
    //lastname.setText(customer.getLname());
    //service.setText(customer.getService());
    month.setText(billPaid.getBill_Month());
    year.setText(billPaid.getBill_Year());
    gross.setText(billPaid.getPaid_Amount());

    return ListViewItem;
}
}

賬單支付.java :

package com.example.umangthakkar.electrofycustomer;

/**
 * Created by Umang Thakkar on 27/04/2018.
 */
public class BillPaid {
private  String Order_Id;
private  String Transaction_Id;
private  String Bill_Id;
private  String Bill_Date;
private  String Paid_Date;
private  String Paid_Amount;
private  String Bill_Month;
private  String Bill_Year;

public BillPaid()
{

}

public BillPaid(String order_Id, String transaction_Id, String bill_Id, String bill_Date, String paid_Date, String paid_Amount, String bill_Month, String bill_Year) {
    Order_Id = order_Id;
    Transaction_Id = transaction_Id;
    Bill_Id = bill_Id;
    Bill_Date = bill_Date;
    Paid_Date = paid_Date;
    Paid_Amount = paid_Amount;
    Bill_Month = bill_Month;
    Bill_Year = bill_Year;
}

public String getOrder_Id() {
    return Order_Id;
}

public void setOrder_Id(String order_Id) {
    Order_Id = order_Id;
}

public String getTransaction_Id() {
    return Transaction_Id;
}

public void setTransaction_Id(String transaction_Id) {
    Transaction_Id = transaction_Id;
}

public String getBill_Id() {
    return Bill_Id;
}

public void setBill_Id(String bill_Id) {
    Bill_Id = bill_Id;
}

public String getBill_Date() {
    return Bill_Date;
}

public void setBill_Date(String bill_Date) {
    Bill_Date = bill_Date;
}

public String getPaid_Date() {
    return Paid_Date;
}

public void setPaid_Date(String paid_Date) {
    Paid_Date = paid_Date;
}

public String getPaid_Amount() {
    return Paid_Amount;
}

public void setPaid_Amount(String paid_Amount) {
    Paid_Amount = paid_Amount;
}

public String getBill_Month() {
    return Bill_Month;
}

public void setBill_Month(String bill_Month) {
    Bill_Month = bill_Month;
}

public String getBill_Year() {
    return Bill_Year;
}

public void setBill_Year(String bill_Year) {
    Bill_Year = bill_Year;
}
}

這是錯誤 m 得到詳細信息

在你的代碼中,你有這個:

dbBillPaid.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        billPaidList.clear();


        for(DataSnapshot customerSnapshot : dataSnapshot.getChildren()){
            // Customer customer = customerSnapshot.getValue(Customer.class);
            BillPaid billPaid =  customerSnapshot.getValue(BillPaid.class);

            billPaidList.add(billPaid);
        }

上面的 for 循環在直接子級內部進行迭代並返回一個字符串,因此您會收到該錯誤。

假設你有這個數據庫:

billpaid
     pushid
        order_id:...

然后去掉循環,使用以下方法解決錯誤:

dbBillPaid.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        billPaidList.clear();

            BillPaid billPaid =  dataSnapshot.getValue(BillPaid.class);

            billPaidList.add(billPaid);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM