带有Firebase的Android:DataSnapshot.getValue()引发java.lang.IllegalArgumentException [英] Android with Firebase : DataSnapshot.getValue() throws java.lang.IllegalArgumentException
问题描述
我正在从Firebase实时数据库中编写和读取类User.这是用户类:
I am writing and reading class User from Firebase realtime database. This is User class :
public class User {
public String name;
public int number;
public String title;
public String company;
public String location;
public Bitmap image;
public String name_title_company_location;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String name, int number, String title, String company, String location, Bitmap image) {
this.name = name;
this.number = number;
this.title = title;
this.company = company;
this.location = location;
this.image = image;
this.name_title_company_location = name + "_" + title + "_" +company + "_" + location;
}
这是我的Firebase实时数据库:
This is my Firebase realtime database:
users
0
company: "Indifair"
image
location: "Bangalore"
name: "Anupam Singh"
name_title_company_location: "Anupam Singh_Engineer_Indifair_Bangalore"
number: 956156494
title: "Engineer"
这段代码抛出错误:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
EditText t1 = (EditText) findViewById(R.id.editText10);
EditText t2 = (EditText) findViewById(R.id.editText11);
EditText t3 = (EditText) findViewById(R.id.editText13);
EditText t4 = (EditText) findViewById(R.id.editText14);
String s1 = t1.getText().toString();
String s2 = t2.getText().toString();
String s3 = t3.getText().toString();
String s4 = t4.getText().toString();
mDatabase.child("users").orderByChild("name_title_company_location")
.equalTo(s1 + "_" + s2 + "_" + s3 + "_" + s4)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
//ImageView im = (ImageView) findViewById(R.id.imageView);
//TextView tv1 = (TextView) findViewById(R.id.textView3);
//TextView tv2 = (TextView) findViewById(R.id.textView5);
///im.setImageBitmap(user.image);
///tv1.setText(user.name);
///tv2.setText(user.number);
}
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
//Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
我收到此错误:
java.lang.IllegalArgumentException: Service not registered: com.google.android.gms.measurement.internal.zzjc@45abe13
at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1105)
at android.app.ContextImpl.unbindService(ContextImpl.java:1873)
at android.content.ContextWrapper.unbindService(ContextWrapper.java:562)
at com.google.android.gms.common.stats.ConnectionTracker.unbindService(Unknown Source)
at com.google.android.gms.measurement.internal.zzik.zzah(com.google.android.gms:play-services-measurement-impl@@17.2.0:240)
at com.google.android.gms.measurement.internal.zzik.zzak(com.google.android.gms:play-services-measurement-impl@@17.2.0:257)
at com.google.android.gms.measurement.internal.zzik.zzc(com.google.android.gms:play-services-measurement-impl@@17.2.0:319)
at com.google.android.gms.measurement.internal.zzij.zza(com.google.android.gms:play-services-measurement-impl@@17.2.0:2)
at com.google.android.gms.measurement.internal.zzai.run(com.google.android.gms:play-services-measurement-impl@@17.2.0:7)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.gms.measurement.internal.zzfy.run(com.google.android.gms:play-services-measurement-impl@@17.2.0:20)
当我注释掉 User user = dataSnapshot.getValue(User.class);
时,错误消失了.
When I comment out User user = dataSnapshot.getValue(User.class);
the error is gone.
出什么问题了?
推荐答案
对Firebase数据库执行查询时,可能会有多个结果.因此,快照包含这些结果的列表.即使只有一个结果,快照也会包含一个结果的列表.
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
因此,您的 onDataChange
需要处理以下事实,即 DataSnapshot
可以包含多个用户.您可以像这样通过遍历 dataSnapshot.getChildren()
来轻松地做到这一点:
So your onDataChange
needs to handle the fact that the DataSnapshot
can contain multiple users. You can easily do this by looping over the dataSnapshot.getChildren()
like this:
mDatabase.child("users").orderByChild("name_title_company_location")
.equalTo(s1 + "_" + s2 + "_" + s3 + "_" + s4)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren) {
User user = userSnapshot.getValue(User.class);
...
}
}
这篇关于带有Firebase的Android:DataSnapshot.getValue()引发java.lang.IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!