无法在 kotlin 中访问子类中的父类变量

Dan*_*arc 1 android android-intent kotlin kotlin-android-extensions

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

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

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 
        }
}



}
Run Code Online (Sandbox Code Playgroud)

为什么我不能在子类中没有父类的构造函数的情况下访问父类变量。给我一些建议,一点帮助将不胜感激

Jig*_*ani 7

inner前补充JavaScriptInterface

就像这样:

inner class JavaScriptInterface(private val mContext: Context) {

    @JavascriptInterface
    fun exampleGet(path: String): String {
        return shared_preferences!!.getString(path, "")
    }
}
Run Code Online (Sandbox Code Playgroud)