qq_39414458
专撸钛合金管
2018-11-10 03:43

android studio用sharedPreference做一个重置密码的功能,但只要点了重置密码的按钮就会闪退是怎么回事

  • android-studio

报错信息
1942-1966/system_process E/ViewRootImpl[myapplication]: Attempting to destroy the window while drawing!
window=android.view.ViewRootImpl@3bcd89a, title=Splash Screen com.example.administrator.myapplication

        1413-1451/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property






                我的代码

    getPassword.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            userName=confirmID.getText().toString().trim();
            psw=newPassword.getText().toString().trim();
            pswAgain=confirmNewPassword.getText().toString().trim();
            if (TextUtils.isEmpty(userName)) {
                Toast.makeText(activity_forget.this,"请输入学号",Toast.LENGTH_SHORT).show();
                return;
            }else if(TextUtils.isEmpty(psw)){
                Toast.makeText(activity_forget.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                return;
            }else if(TextUtils.isEmpty(pswAgain)){
                Toast.makeText(activity_forget.this, "请确认新密码", Toast.LENGTH_SHORT).show();
                return;
            }else if(!psw.equals(pswAgain)){
                Toast.makeText(activity_forget.this, "请确认你的密码", Toast.LENGTH_SHORT).show();
                return;
                /**
                 *从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名
                 */
            }else if(!isExistUserName(userName)){
                Toast.makeText(activity_forget.this, "此学号不存在", Toast.LENGTH_SHORT).show();
                return;
            }else{
                Toast.makeText(activity_forget.this, "注册成功", Toast.LENGTH_SHORT).show();
                //把账号、密码和账号标识保存到sp里面
                /**
                 * 保存账号和密码到SharedPreferences中
                 */
                saveRegisterInfo(userName, psw);
                //注册成功后把账号传递到LoginActivity.java中
                // 返回值到loginActivity显示
                Intent data = new Intent();
                data.putExtra("userName", userName);
                setResult(RESULT_OK, data);
                //RESULT_OK为Activity系统常量,状态码为-1,
                // 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值
                activity_forget.this.finish();
            }
        }
    });


            下面是两个用到的函数
            private boolean isExistUserName(String userName){
    boolean has_userName=false;
    //mode_private SharedPreferences sp = getSharedPreferences( );
    // "loginInfo", MODE_PRIVATE
    SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
    //获取密码
    String spPsw=sp.getString(userName, "");//传入用户名获取密码
    //如果密码不为空则确实保存过这个用户名
    if(!TextUtils.isEmpty(spPsw)) {
        has_userName=true;
    }
    return has_userName;
}
private void saveRegisterInfo(String userName,String psw){
    String md5Psw = MD5Utils.md5(psw);//把密码用MD5加密
    //loginInfo表示文件名, mode_private SharedPreferences sp = getSharedPreferences( );
    SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
    //获取编辑器, SharedPreferences.Editor  editor -> sp.edit();
    SharedPreferences.Editor editor=sp.edit();
    //以用户名为key,密码为value保存在SharedPreferences中
    //key,value,如键值对,editor.putString(用户名,密码);
    editor.putString(userName, md5Psw);
    //提交修改 editor.commit();
    editor.commit();
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答