NullPointerException 正在发生,我真的不知道我做错了什么。请帮我。而且我也是新手
NullPointerException is occuring and I really don't have any clue of what I am doing wrong. Please help me. And I'm also a newbie
SessioManager.kt
class SessionManager(context : Context) {
private var PRIVATE_MODE = 0
val PREF_NAME = "LoginLogout"
val KEY_IS_LOGGEDIN = "isLoggedIn"
var pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE) //---------error
var pref_editor: SharedPreferences.Editor = pref.edit()
fun setLogin(isLoggedIn: Boolean) { //use to login or logout
pref_editor.putBoolean(KEY_IS_LOGGEDIN, isLoggedIn)
pref_editor.apply()
if(!isLoggedIn) {
pref_editor.clear().apply()
}
}
fun isLoggedIn() : Boolean {
return pref.getBoolean(KEY_IS_LOGGEDIN, false)
}
}
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
private SessionManager sessionManager = new SessionManager(HomeActivity.this); //-------error
private SharedPreferences sharedPreferences = sessionManager.getPref();
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
Android 中的活动与典型的 class 不同,因为您不会自己实例化它们。它们由 OS 实例化,然后 OS 在您访问它之前设置对象。
但是当您在声明站点初始化 SessionManager
时,实际上是在向构造函数中添加代码,因此您可以在 OS 之前提前访问 class完成设置。
在这种情况下,您将 this
作为 Context
对象在准备就绪之前传递。
基本经验法则:如果构造函数需要 Context
参数,请不要在 property/field 的声明位置调用它。在 Kotlin 中,您必须将 属性 设为 lateinit var
并在 onCreate()
中实例化它或将其设为 Lazy
。我认为 lazy 更干净,因为它可以让你使用 read-only val
:
val sessionManager: SessionManager by lazy { SessionManager(this) }
在Java中,你必须使用onCreate
方法:
private SessionManager sessionManager;
//...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sessionManager = new SessionManager(this);
}
SessioManager.kt
class SessionManager(context : Context) {
private var PRIVATE_MODE = 0
val PREF_NAME = "LoginLogout"
val KEY_IS_LOGGEDIN = "isLoggedIn"
var pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE) //---------error
var pref_editor: SharedPreferences.Editor = pref.edit()
fun setLogin(isLoggedIn: Boolean) { //use to login or logout
pref_editor.putBoolean(KEY_IS_LOGGEDIN, isLoggedIn)
pref_editor.apply()
if(!isLoggedIn) {
pref_editor.clear().apply()
}
}
fun isLoggedIn() : Boolean {
return pref.getBoolean(KEY_IS_LOGGEDIN, false)
}
}
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
private SessionManager sessionManager = new SessionManager(HomeActivity.this); //-------error
private SharedPreferences sharedPreferences = sessionManager.getPref();
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
Android 中的活动与典型的 class 不同,因为您不会自己实例化它们。它们由 OS 实例化,然后 OS 在您访问它之前设置对象。
但是当您在声明站点初始化 SessionManager
时,实际上是在向构造函数中添加代码,因此您可以在 OS 之前提前访问 class完成设置。
在这种情况下,您将 this
作为 Context
对象在准备就绪之前传递。
基本经验法则:如果构造函数需要 Context
参数,请不要在 property/field 的声明位置调用它。在 Kotlin 中,您必须将 属性 设为 lateinit var
并在 onCreate()
中实例化它或将其设为 Lazy
。我认为 lazy 更干净,因为它可以让你使用 read-only val
:
val sessionManager: SessionManager by lazy { SessionManager(this) }
在Java中,你必须使用onCreate
方法:
private SessionManager sessionManager;
//...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sessionManager = new SessionManager(this);
}