无法访问Kotlin的子类中的父类变量

时间:2019-07-26 07:50:17

标签: android android-intent kotlin kotlin-android-extensions

在Kotlin中,我尝试在子类中使用父类变量,但我无法使用它们,因为我是Kotlin的新手,我不知道如何简单地做到这一点

我正在尝试访问sharedPerfernces并获取但它给了我空值

class webViewActivity : AppCompatActivity{

 internal var shared_preferences: SharedPreferences? = null


override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        shared_preferences = this.getPreferences(Context.MODE_PRIVATE)

        mContext = this
    }


class JavaScriptInterface(private val mContext: Context) {

 @JavascriptInterface
        fun exampleGet(path: String): String {
            return webViewActivity().shared_preferences!!.getString(path, "")

//here shared_perferences is null 
        }
}



}

为什么在子类中没有父类的构造函数就无法访问父类变量。给我一些建议,小小的帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

inner类之前添加JavaScriptInterface

就是这样:

inner class JavaScriptInterface(private val mContext: Context) {

    @JavascriptInterface
    fun exampleGet(path: String): String {
        return shared_preferences!!.getString(path, "")
    }
}
相关问题