Condition is not working in sharedpreferences andr

2019-10-03 11:22发布

问题:

I have a Login page, so for that I am using SharedPreferences. I am trying to save values. Without values, it shouldn't go to the another Activity for that I have given this in Login page:

 if((spf != null) && (spv !=null) && (et1v != null) && (et2v != null) && (et3v != null) &&

             (sp != null) && (et1 != null) && (et2 != null) && (et3 != null)){

            finish();
            Intent i = new Intent(Login.this,Welcome.class);
            startActivity(i);
     }
       else{
           btn1.setOnClickListener(
                   new View.OnClickListener() {
                       @Override
                       public void onClick(View view) {

                           if (sp.getSelectedItem().toString().length() > 0 &&
                                   et1.getText().toString().length() > 0 &&
                                   et2.getText().toString().length() > 0 &&
                                   et3.getText().toString().length() > 0 )

                           {
                               SharedPreferences spf = getSharedPreferences("newprfs", Context.MODE_PRIVATE);
                               SharedPreferences.Editor spe = spf.edit();
                               spe.putString("uname",sp.getSelectedItem().toString());
                               spe.putString("password",et1.getText().toString());
                               spe.putString("mobile",et2.getText().toString());
                               spe.putString("dept",et3.getText().toString());

                               spe.commit();

                               finish();
                               Intent i = new Intent(Login.this,Welcome.class);
                               startActivity(i);
                           }
                       }
                   }
           );
        }

this my Welcome page

SharedPreferences spf = getSharedPreferences("newprfs", Context.MODE_PRIVATE);
        SharedPreferences.Editor spe = spf.edit();
        String spv = spf.getString("uname","");
        String et1v = spf.getString("password","");
        String et2v = spf.getString("mobile","");
        String et3v = spf.getString("dept","");

        if((spe != null) && (spv !=null) && (et1v != null) && (et2v != null) && (et3v != null) &&
                (sp != null) && (et1 != null) && (et2 != null) && (et3 != null)){
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                    Intent i = new Intent(Welcome.this,MainActivity.class);
                    startActivity(i);
                    Welcome.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
        else{
            Intent i = new Intent(Welcome.this,Login.class);
            startActivity(i);
            Welcome.this.finish();
        }
    }       

The problem is that it always showing a blank screen. I tried to check with Log.d("@@@@@@@@@@@@@@Loginpage@@@@@@@@@@@@@@@@"); so i recognised to its loading always in Login page and if condition is not working.. can any one suggest me whats wrong in if Condition..?

Update Here Its not Problem with Shared Prefs.. But here Its the Problem with Condition...

I the If Condition without Entering Values it should not move to Next... and Also if values already Defined previously it should Move to next page...

So Its moving to next page and In next page Also Same happening Because I have given same condition and Again Its moving to Login page again. So it stays on the Single page and I am getting blank Screen.

回答1:

You need to try with contains(String Key)

Try this spv.contains("uname") instead of (spv !=null) or spv.equals("") I also Faced this issue Better To use Hashmap... but this works for your Case..