Java 如何修复SharedReference以保存数据并跳过活动?
我想使用“共享首选项”保存我的用户名和密码,它已保存,但在我第二次打开应用程序时不起作用Java 如何修复SharedReference以保存数据并跳过活动?,java,android,Java,Android,我想使用“共享首选项”保存我的用户名和密码,它已保存,但在我第二次打开应用程序时不起作用 import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Typeface; import android.icu.text.IDNA; import android.support.v4.media.RatingCompat; import android.s
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.icu.text.IDNA;
import android.support.v4.media.RatingCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Login_page extends AppCompatActivity {
TextView tv_user, tv_pass;
EditText tx_user, tx_pass;
Button btn_login;
ImageView img_ghorme;
SharedPreferences save;
public void findall()
{
tv_user = (TextView)findViewById(R.id.tv_user);
tv_pass = (TextView)findViewById(R.id.tv_pass);
tx_user = (EditText)findViewById(R.id.tx_user);
tx_pass = (EditText)findViewById(R.id.tx_pass);
btn_login = (Button)findViewById(R.id.btn_login);
img_ghorme = (ImageView)findViewById(R.id.img_ghorme);
}
public String save_user = "";
public String save_pass = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page2);
findall();
if(save_user.equals("admin") && save_pass.equals("admin"))
{
Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = tx_user.getText()+"";
String pass = tx_pass.getText()+"";
save_user = save.getString("username", "");
save_pass = save.getString("password", "");
SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();
if(user.equals("admin") && pass.equals("admin"))
{
Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);
}
if (!user.equals("admin") || !pass.equals("admin"))
{
e.putString("username", "");
e.putString("password", "");
e.apply();
tx_user.setText("");
tx_pass.setText("");
}
}
});
save = getSharedPreferences("user", MODE_PRIVATE);
save = getSharedPreferences("pass", MODE_PRIVATE);
Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);
Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);
tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);
tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);
tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);
tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);
btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);
img_ghorme.startAnimation(ani_dtu);
}
@Override
protected void onPause() {
super.onPause();
finish();
}
}
XML:
我希望当我为用户名和密码编写admin时,它会保存它,第二次打开应用程序时,它会跳过该登录页面,但它不工作,并再次显示登录页面在使用它之前,您不会初始化SharedReference,也不会为每个密钥对定义两个SharedReference。更改您的代码如下
public class Login_page extends AppCompatActivity {
TextView tv_user, tv_pass;
EditText tx_user, tx_pass;
Button btn_login;
ImageView img_ghorme;
SharedPreferences save;
public void findall()
{
tv_user = findViewById(R.id.tv_user);
tv_pass = findViewById(R.id.tv_pass);
tx_user = findViewById(R.id.tx_user);
tx_pass = findViewById(R.id.tx_pass);
btn_login = findViewById(R.id.btn_login);
img_ghorme = findViewById(R.id.img_ghorme);
}
public String save_user = "";
public String save_pass = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findall();
save = getSharedPreferences("userInfo", MODE_PRIVATE);
save_user = save.getString("username", "");
save_pass = save.getString("password", "");
if(save_user.equals("admin") && save_pass.equals("admin"))
{
Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
String user = tx_user.getText().toString();
String pass = tx_pass.getText().toString();
SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();
if(user.equals("admin") && pass.equals("admin"))
{
Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);
}
if (!user.equals("admin") || !pass.equals("admin"))
{
e.putString("username", "");
e.putString("password", "");
e.apply();
tx_user.setText("");
tx_pass.setText("");
}
}
});
Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);
Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);
tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);
tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);
tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);
tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);
btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);
img_ghorme.startAnimation(ani_dtu);
}
@Override
protected void onPause() {
super.onPause();
finish();
}
}这是创建时的完整代码吗?如何初始化
save
并读取save\u user
和save\u pass
?将整个活动代码张贴在此处。@S-Sh这不是onCreate的漏洞。我用onCreate创建save并在onCreate上初始化它。请同时添加代码,看看如何初始化save@Kabir当我第二次打开应用程序时,当它想通过检查usrename和密码来更改活动时,我添加了我的代码,它强制stoped.yes其崩溃的java.lang.RuntimeException:无法启动活动ComponentInfo{com.home.seesion10/com.home.seesion10.Login_page}:java.lang.NullPointerException:尝试调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)'在空对象引用上,然后设置公共字符串save_user=“”;公共字符串save_pass=“”;不堪一击。它不会崩溃,但不会第二次跳过登录页。我已经更新了答案。只有在您第一次输入用户名和密码时,才会跳过登录页面。
public class Login_page extends AppCompatActivity {
TextView tv_user, tv_pass;
EditText tx_user, tx_pass;
Button btn_login;
ImageView img_ghorme;
SharedPreferences save;
public void findall()
{
tv_user = findViewById(R.id.tv_user);
tv_pass = findViewById(R.id.tv_pass);
tx_user = findViewById(R.id.tx_user);
tx_pass = findViewById(R.id.tx_pass);
btn_login = findViewById(R.id.btn_login);
img_ghorme = findViewById(R.id.img_ghorme);
}
public String save_user = "";
public String save_pass = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findall();
save = getSharedPreferences("userInfo", MODE_PRIVATE);
save_user = save.getString("username", "");
save_pass = save.getString("password", "");
if(save_user.equals("admin") && save_pass.equals("admin"))
{
Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
String user = tx_user.getText().toString();
String pass = tx_pass.getText().toString();
SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();
if(user.equals("admin") && pass.equals("admin"))
{
Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);
}
if (!user.equals("admin") || !pass.equals("admin"))
{
e.putString("username", "");
e.putString("password", "");
e.apply();
tx_user.setText("");
tx_pass.setText("");
}
}
});
Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);
Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);
tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);
tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);
tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);
tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);
btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);
img_ghorme.startAnimation(ani_dtu);
}
@Override
protected void onPause() {
super.onPause();
finish();
}