共享参考Java Android Studio
我试图在用户登录/注册时创建一个用户会话,以便他们在关闭应用程序后自动进入该应用程序 我不断发现这个错误:共享参考Java Android Studio,java,android,Java,Android,我试图在用户登录/注册时创建一个用户会话,以便他们在关闭应用程序后自动进入该应用程序 我不断发现这个错误: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at com.example
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context
android.content.Context.getApplicationContext()' on a null object reference
at com.example.myapplication.BackgroundWorker.<init>(BackgroundWorker.java:32)
at com.example.myapplication.LoginActivity.OnLogin(LoginActivity.java:73)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener
这是第二个:
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
我的猜测是变量没有保存到SharedRef。内联变量初始值设定项在构造函数体运行之前被调用。因此,当您尝试初始化
首选项时,上下文
的值仍然为空,因为尚未调用构造函数。在文件中,将初始值设定项放在构造函数之后并不重要,它仍然会首先被调用。您应该在构造函数中初始化它
此外,不要只保留对SharedReferences.Editor
的引用。需要时在本地创建它
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
SharedPreferences preferences;
AlertDialog alertDialog;
BackgroundWorker(Context ctx) {
context = ctx;
preferences = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
}
@Override
protected String doInBackground(String... params) {
SharedPreferences.Editor editor = preferences.edit();
String type = params [0];
String login_url = "http://192.168.1.37/byebye/login_handler.php";
String register_url = "http://192.168.1.37/byebye/register_handler.php";
// ...
}
// ...
}
公共类BackgroundWorker扩展异步任务{
语境;
共享参考偏好;
警报对话框警报对话框;
BackgroundWorker(上下文ctx){
上下文=ctx;
preferences=PreferenceManager.GetDefaultSharedReferences(ctx.getApplicationContext());
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
SharedReferences.Editor=首选项.edit();
字符串类型=参数[0];
字符串登录\u url=”http://192.168.1.37/byebye/login_handler.php";
字符串寄存器\u url=”http://192.168.1.37/byebye/register_handler.php";
// ...
}
// ...
}
您应该在活动类中初始化您的共享引用。因为任务在后台工作。因此,当你的应用程序关闭时,任务仍然处于活动状态,但你的应用程序不在前台。这就是为什么getApplicationContext()
提供空值。因此,为了防止此错误,请在activityonCreate()
方法中初始化SharedReference
,然后使用它。在调用BackgroundWorker
的位置发布代码。您可能正在向其传递空上下文。公共类BackgroundWorker扩展异步任务{SharedReferences SharedReferences;上下文上下文;AlertDialog AlertDialog;BackgroundWorker(上下文ctx){context=ctx;首选项=PreferenceManager.GetDefaultSharedReferences(context.getApplicationContext();}SharedReferences首选项=首选项管理器.getDefaultSharedReferences(context.getApplicationContext())
我还将此SharedReferences SharedReferences;
放在上下文上下文的下方和上方;
在我的LoginActivity中?是的,您可以在LoginActivity onCreate方法中初始化
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
SharedPreferences preferences;
AlertDialog alertDialog;
BackgroundWorker(Context ctx) {
context = ctx;
preferences = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
}
@Override
protected String doInBackground(String... params) {
SharedPreferences.Editor editor = preferences.edit();
String type = params [0];
String login_url = "http://192.168.1.37/byebye/login_handler.php";
String register_url = "http://192.168.1.37/byebye/register_handler.php";
// ...
}
// ...
}