问题
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;
}
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"
This piece of code throws error :
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());
}
});
I am getting this error :
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)
When I comment out User user = dataSnapshot.getValue(User.class); the error is gone.
What could be going wrong?
回答1:
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.
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);
...
}
}
来源:https://stackoverflow.com/questions/59600313/android-with-firebase-datasnapshot-getvalue-throws-java-lang-illegalargument