这是我过去两天所面对的错误。
请给我相同的解决方案
过程: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(未知来源)
at 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)
at 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;
}
}
BIllPaid.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;
}
}
答案 0 :(得分:1)
在你的代码中,你有这个:
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循环在直接子节点内迭代并返回一个String,因此你得到了这个错误。
假设你有这个db:
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);
}