java.lang.String cannot be cast to java.lang.Long firebase realtime

YPLABS :

I'm having this error does anyone know how to fix?

java.lang.String cannot be cast to java.lang.Long

LOGCAT

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
        at com.appcom.Cassino$1.onDataChange(Cassino.java:68)
        at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

CODE /LINE ERROR

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    long v= (long) dataSnapshot.getValue(); // This is the line of error
    CountDownTimer countDownTimer=new CountDownTimer(v,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mQnATimer.setText("time : "+millisUntilFinished/1000);
        }
        @Override
        public void onFinish() {

        }
    }.start();
}
Reaz Murshed :

It looks like a pretty straightforward problem. However, I cannot tell for sure without seeing your actual data structure.

From the logcat, it looks like you are getting a String value and while you are trying to cast it to the long you are getting an exception.

I would recommend doing the following in that case.

Long v = Long.parseLong((String) dataSnapshot.getValue());

If the String does not have a valid Long, then it should throw another exception though. However, as I said, it is difficult to understand the exact problem without seeing your database structure with some sample data.

I hope that helps.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=193138&siteId=1