Kotlin 中的分配变量无法正确解析

Assigned Variable in Kotlin Doesn't Resolve Correctly

我遇到一个问题,其中 var instance: IbeaconTooth? = null 用 "Do not place Android context classes in static fields" 突出显示。我尝试了我能想到的一切来解决这个问题,但似乎没有任何效果。每当我 运行 应用程序时,我收到的错误是 "ibeantooth is null , please use init() method"。感谢任何帮助。

IbeaconTooth.kt

class IbeaconTooth constructor(mContext: Context) {

        private val cxt: Context = mContext
        private val manager: BluetoothManager = cxt.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        private val adapter: BluetoothAdapter = manager.adapter
        private var scanner: BluetoothLeScanner? = null
        private var isScan: Boolean = false
        private val TAG = "beacon"
        // scan callback
        private var bleCallback: BeaconBleCallback? = null
        private var leCallback: BeaconLeCallback? = null

        init {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                scanner = adapter.bluetoothLeScanner
            }
        }

        companion object {

            var instance: IbeaconTooth? = null

            fun init(context: Context) {
                if (instance == null) {
                    synchronized(IbeaconTooth::class.java) {
                        if (instance == null) {
                            instance = IbeaconTooth(context)
                        }
                    }
                }
            }

            fun getIbeacon(): IbeaconTooth {
                if (instance == null) {
                    throw IllegalArgumentException("ibeantooth is null , please use init() method")
                }
                return instance as IbeaconTooth
            }

        }

        fun startBeacon(onBeaconScanListener: OnBeaconScanListener) {
            if (isScan) {
                Log.d(TAG, "already start scan")
                onBeaconScanListener.onScanErrorMsg("start scan already")
            } else {
                if (scanner == null) {
                    bleCallback = BeaconBleCallback(onBeaconScanListener)
                    adapter.startLeScan(bleCallback)
                } else {
                    // android 5.0
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        leCallback = BeaconLeCallback(onBeaconScanListener)
                        scanner?.startScan(leCallback)
                    }
                }
            }
        }

        fun stopBeacon() {
            if (isScan) {
                // stop scan
                if (scanner == null) {
                    if (bleCallback != null) {
                        adapter.stopLeScan(bleCallback)
                    }
                } else {
                    if (leCallback != null) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            scanner?.stopScan(leCallback)
                        }
                    }
                }
            } else {
                Log.d(TAG, "already stop scan")
            }
        }
    }

ActivateBeacon.kt

class ActivateBeacon : AppCompatActivity(), OnBeaconScanListener {
    private lateinit var rippleView: RippleView

    override fun OnScanResult(beacon: Beacon) {
        Log.v("beacon", "result > $beacon")
        rippleView.newRipple()

    }

    override fun onScanErrorMsg(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }

    override fun getScanFilter(): BeaconFilter = BeaconFilter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ibeacon)
        rippleView = findViewById(R.id.circle_ripple)
        rippleView.newRipple()
    }
    override fun onResume() {
        super.onResume()
        IbeaconTooth.getIbeacon().startBeacon(this)

    }

    override fun onDestroy() {
        super.onDestroy()
        IbeaconTooth.getIbeacon().stopBeacon()
    }

    override fun onBackPressed() {
        finish()
    }
}
    companion object {

        var instance: IbeaconTooth? = null

        fun init(context: Context) {
            if (instance == null) {
                synchronized(IbeaconTooth::class.java) {
                    if (instance == null) {
                        instance = IbeaconTooth(context)
                    }
                }
            }
        }

        fun getIbeacon(context:Context): IbeaconTooth {
            if (instance == null) {
                instance = init(context)
            }
            return instance as IbeaconTooth
        }

    }

这将消除在 get 之前调用 init 的开销。