Kotlin Aar Android库崩溃

时间:2018-07-07 07:32:15

标签: android kotlin aar

我现在尝试构建aar库以获取设备信息。 构建库似乎很好,但是当我将测试应用程序与库集成时,我会崩溃。

这是我的崩溃日志。

07-07 02:44:23.033 8021-8021/ruri.sdk_test E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ruri.sdk_test, PID: 8021
    java.lang.ClassCastException: java.lang.String cannot be cast to android.net.ConnectivityManager
        at jinuooki.com.peterworks.peterBrix$Companion.event(peterBrix.kt:43)
        at jinuooki.com.peterworks.peterBrix.event(Unknown Source:2)
        at ruri.sdk_test.MainActivity$1.onClick(MainActivity.java:20)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是我的图书馆代码。

fun event(eventName: String) {

        val osName = "Android"

        val connect: ConnectivityManager = Context.CONNECTIVITY_SERVICE
        val netStatue = connect.activeNetworkInfo.typeName
        val networkString = netStatue.toLowerCase()

        val wm: WindowManager = Context.WINDOW_SERVICE
        val rotation = wm.defaultDisplay.rotation
        val sMode = when (rotation) {
            Surface.ROTATION_0 -> true
            Surface.ROTATION_180 -> true
            Surface.ROTATION_90 -> false
            Surface.ROTATION_270 -> false
            else -> null
        }

        val telephonyManager = Context.TELEPHONY_SERVICE
        val netwokrName = telephonyManager

        val app = PackageManager.GET_META_DATA as ApplicationInfo
        val bundle = app.metaData

        val packagename = app.packageName

    }

你能告诉我我想念什么吗?

1 个答案:

答案 0 :(得分:0)

stacktrace与您发布的代码不匹配。

错误消息:

  

java.lang.String无法转换为android.net.ConnectivityManager

意味着您有一个String,正试图投射为ConnectivityManager

请务必小心,以正确的方式获取ConnectivityManager

根据官方指南here,您应该使用:

val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager