'Not a direct subclass of GenericTypeIndicator: class com.google.firebase.database.GenericTypeIndicator
I have Firebase App with Realtime Database, I have db.json as
{
"brs" : {
"route": [
{
"routeDestination": "DDDD1",
"routeOrigin": "OOOO1",
"bus" : {
"busArrivalTime" : "created_at_timestamp",
"busDepartureTime" : "created_at_timestamp",
"busName" : "SOME NAME",
"busSeatCost" : "0000",
"busTotalSeats" : "000",
"reservations": [
{
"reservationId": 1,
"reservationDate": "Wed Jul 06 23:54:56 EDT 2016",
"seats": [
{
"seatNumber": 1
},
{
"seatNumber": 2
}
]
}
]
}
},
{
"routeDestination": "DDDD2",
"routeOrigin": "OOOO2",
"bus" : {
"busArrivalTime" : "created_at_timestamp",
"busDepartureTime" : "created_at_timestamp",
"busName" : "SOME NAME",
"busSeatCost" : "0000",
"busTotalSeats" : "000",
"reservations": [
{
"reservationId": 1,
"reservationDate": "Wed Jul 06 23:54:56 EDT 2016",
"seats": [
{
"seatNumber": 1
},
{
"seatNumber": 2
}
]
}
]
}
},
{
"routeDestination": "DDDD3",
"routeOrigin": "OOOO3",
"bus" : {
"busArrivalTime" : "created_at_timestamp",
"busDepartureTime" : "created_at_timestamp",
"busName" : "SOME NAME",
"busSeatCost" : "0000",
"busTotalSeats" : "000",
"reservations": [
{
"reservationId": 1,
"reservationDate": "Wed Jul 06 23:54:56 EDT 2016",
"seats": [
{
"seatNumber": 1
},
{
"seatNumber": 2
}
]
}
]
}
}
]
}
}
Now in android App I want to retrieve the routes in a list as
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference myRef = firebaseDatabase.getReference("route");
try {
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
GenericTypeIndicator t = new GenericTypeIndicator () {};
List<Object> routes = (List<Object>) dataSnapshot.getValue(t);
if( routes == null ) {
Snackbar.make(view, "No value " ,Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
else {
Snackbar.make(view, "The routes Size is " + routes.size(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("BUS_TAG", "Failed to read value.", error.toException());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
through this code I'm getting
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rhcloud.escot.bsr, PID: 1750
com.google.firebase.database.DatabaseException: Not a direct subclass of GenericTypeIndicator: class com.google.firebase.database.GenericTypeIndicator
at com.google.android.gms.internal.zzalq.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.rhcloud.escot.bsr.MainActivity$2$1.onDataChange(MainActivity.java:61)
at com.google.android.gms.internal.zzaih.zza(Unknown Source)
at com.google.android.gms.internal.zzajh.zzctc(Unknown Source)
at com.google.android.gms.internal.zzajk$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
07-20 09:07:05.756 757-1407/? W/ActivityManager: Force finishing activity 1 com.rhcloud.escot.bsr/.MainActivity
are there any parts which I'm missing here to read routes list.
Solution 1:[1]
instead of this:
GenericTypeIndicator t = new GenericTypeIndicator () {};
List<Object> routes = (List<Object>) dataSnapshot.getValue(t);
use this
GenericTypeIndicator<List<Object>> t = new GenericTypeIndicator <List<Object>>() {};
List<Object> routes = dataSnapshot.getValue(t);
Solution 2:[2]
I had this same issue today and got fixed by adding this line to ProGoard / R8 config:
-keepclassmembers class com.google.firebase.database.GenericTypeIndicator { *; }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mrk_Slk |
Solution 2 | Vlad |