问题描述
这是代码:
val gps_enabled = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)
if(!gps_enabled) {
Log.d("letsSee", "GPS IS OFF")
}else{
Log.d("letsSee", "GPS IS ON")
}
当我尝试检查GPS是否开启时,应用程序崩溃。 没有检查,一切正常,但我仍然需要检查! 有任何想法吗?
09-16 03:59:12.533 32454-32454/com.amoflirt.amoflirt E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
09-16 03:59:13.713 32454-32454/com.amoflirt.amoflirt E/AndroidRuntime: FATAL EXCEPTION: main
kotlin.KotlinNullPointerException
at com.amoflirt.amoflirt.MainActivity.getLocation(MainActivity.kt:95)
at com.amoflirt.amoflirt.MainActivity$onCreate$3.onClick(MainActivity.kt:67)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
1楼
在访问之前,请确保已定义locationManager
。
附带说明一下,您也许可以使用Kotlin的惰性init:
val locationManager by lazy { //however you define it normally }
那只会在第一次访问它时对其进行初始化,然后在该类的生命周期内保留该实例。