gpt4 book ai didi

java - 在登录类的共享首选项中保存字符串以在 MainActivity 中使用 - 无法引用定义的非最终局部变量编辑器

转载 作者:行者123 更新时间:2023-12-01 10:21:56 25 4
gpt4 key购买 nike

我已经构建了一个登录名,我希望我的应用程序将登录时使用的用户名存储为 session 名称,以便我可以根据登录者从数据库中提取信息。

登录工作正常,但是我第一次使用共享首选项将变量保存在 Login.java 中,然后我将在 MainActivity.java 中调用它> 运行 SQL 语句。

这是我的Login.java - 我首先尝试使用 Intent 来发送 boolean session 用户名,但我不能'不太了解 MainActivity 中已经存在的逻辑(onResume() 方法),因此我决定使用共享首选项,但收到错误 Cannot refer to the non-final local variable editor defined in an enclosing scope下划线 editor.在 if 语句中。

基本上:

1) 我应该使用我已经尝试实现的 Intent 来存储 session 名称和 boolean 值,还是使用共享首选项?如果是这样

2)如果答案是sharedpreferences,为什么它不让我使用editor.putStringeditor.apply();在 onClick 中?

请帮忙!有点头疼哈哈。

public class Login extends Activity   {
DB db = new DB(this);
boolean session = false;
int visited = 1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

final EditText usernameET=(EditText)findViewById(R.id.usernameET);
final EditText passwordET=(EditText)findViewById(R.id.passwordET);
final Button loginBTN=(Button)findViewById(R.id.loginBTN);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();

loginBTN.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String username = usernameET.getText().toString();
String password = passwordET.getText().toString();
boolean success = db.Login(username, password);

if(success)
{
Log.d("login", "user logged");
session = true;
Intent i = new Intent(Login.this, MainActivity.class);
i.putExtra("loginSuccess", session);
i.putExtra("sessionName", username);
editor.putString("Name", username);
editor.apply();
startActivity(i);

}
else
{
Log.d("login", "user not logged");
}
}

});





}

}

这是我的MainActivity.java

onResume()方法

此方法中的错误强调 getBooleanExtraThe method getBooleanExtra(String, boolean) in the type Intent is not applicable for the arguments (String)

protected void onResume() {
super.onResume();
final Intent intent = getIntent();
if (intent.hasExtra("loginSuccess")) {
boolean loginSuccess = intent.getBooleanExtra("loginSuccess");
//Code goes here
}
}

最佳答案

问题 1:方法 getBooleanExtra(String, boolean)类型 Intent不适用于参数(字符串)

getBooleanExtra的方法签名要求两个参数,但您只传递一个。第一个是键,第二个是默认值(如果未找到该键)。

改变

boolean loginSuccess = intent.getBooleanExtra("loginSuccess");

boolean loginSuccess = intent.getBooleanExtra("loginSuccess", false); // or true whichever you want as your default value

问题 2:无法引用封闭范围内定义的非最终局部变量编辑器

只需输入 final username之前的标识符

final String username = usernameET.getText().toString();

Should I use the intents I have already tried to implement to store a session name and boolean, or do I use sharedpreferences?

我会使用SharedPreference因为它不会强制我通过 Intent 向每个需要 session 信息的 Activity 发送数据。我将能够在任何地方获得它,而无需付出任何额外的努力(每次向每个 Activity 发送数据)

关于java - 在登录类的共享首选项中保存字符串以在 MainActivity 中使用 - 无法引用定义的非最终局部变量编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35539607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com