0

I am trying to call function sleep from my Utils as the below code:

@Override
                        public void onError(String err) {
                            log("A pinger died");
                            if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_FAIL)) {
                                PingStream.this.onError(err);
                                return;
                            }
                            if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_ATTEMPT_RESTART) || errorHandlingMode.equals(SpeedtestConfig.ONERROR_MUST_RESTART)) {
                                Utils.sleep(100);
                                init();
                            }
                        }

and this is the full function I have:

private void init() {
        if (stopASAP) return;
        if (c != null) {
            try {
                c.close();
            } catch (Throwable ignored) {
            }
        }
        new Thread() {
            public void run() {
                if (pinger != null) pinger.stopASAP();
                if (remainingPings <= 0) return;
                try {
                    c = new Connection(server, connectTimeout, soTimeout, recvBuffer, sendBuffer);
                    if (stopASAP) {
                        try {
                            c.close();
                        } catch (Throwable ignored) {
                        }
                        return;
                    }
                    pinger = new Pinger(c, path) {
                        @Override
                        public boolean onPong(long ns) {
                            boolean r = PingStream.this.onPong(ns);
                            if (--remainingPings <= 0 || !r) {
                                onDone();
                                return false;
                            } else return true;
                        }

                        @Override
                        public void onError(String err) {
                            log("A pinger died");
                            if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_FAIL)) {
                                PingStream.this.onError(err);
                                return;
                            }
                            if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_ATTEMPT_RESTART) || errorHandlingMode.equals(SpeedtestConfig.ONERROR_MUST_RESTART)) {
                                Utils.sleep(100);
                                init();
                            }
                        }
                    };
                } catch (Throwable t) {
                    log("A pinger failed hard");
                    try {
                        c.close();
                    } catch (Throwable ignored) {
                    }
                    if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_MUST_RESTART)) {
                        Utils.sleep(100);
                        init();
                    } else onError(t.toString());
                }
            }
        }.start();
    }

I found a compile time error with Non-static 'sleep(long)' cannot be referenced from a static context

and this is the below Utils I have:

package com.live.flutter_telephony_rf_example.core.base

import java.net.URLEncoder

object Utils {
    fun urlEncode(s: String?): String? {
        return try {
            URLEncoder.encode(s, "utf-8")
        } catch (t: Throwable) {
            null
        }
    }

    fun sleep(ms: Long) {
        try {
            Thread.sleep(ms)
        } catch (t: Throwable) {
        }
    }

    fun sleep(ms: Long, ns: Int) {
        try {
            Thread.sleep(ms, ns)
        } catch (t: Throwable) {
        }
    }

    fun url_sep(url: String): String {
        return if (url.contains("?")) "&" else "?"
    }
}

Screen shot error: enter image description here

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
Mahmoud Al-Haroon
  • 2,239
  • 7
  • 36
  • 72

1 Answers1

1

The problem here is that Kotlin doesn't have static methods at all. So calling Utils.sleep(...) isn't a valid approach.

Instead Kotlin has objects, which are treated and instantiated as singletons. Therefore when calling from Java you need the extra step of retrieving the singleton instance which Kotlin exposes as INSTANCE. So something like this:

Utils.INSTANCE.sleep(...);

As an alternative solution, you could also use the @JvmStatic annotation which will allow the method to be interpreted as static:

@JvmStatic
fun sleep(...)
Henry Twist
  • 5,666
  • 3
  • 19
  • 44