I made a class to save the users password into a sharedPrefences(Password.PA开发者_StackOverflow中文版SSWORD_PREF_KEY,0) which works perfectly but my ctivity that they login with is foreclosing on startup. I linked the code if anyone can tell meat i did wrong.
Here is code:
import android.app.Activity;
import android.content.intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LogIn extends Activity {
private EditText pass1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.password);
SharedPreferences passwdfile = getSharedPreferences(Password.PASSWORD_PREF_KEY,1);
final String p3 = passwdfile.getString(Password.PASSWORD_PREF_KEY, null);
final String p1 = pass1.getText().toString();
Button page1 = (Button) findViewById(R.id.btn_login);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (p3.equals(p1)) {
startActivity(new Intent(LogIn.this,Main.class)); finish();
}
else {
Toast.makeText(getApplicationContext(),"Incorrect Password",Toast.LENGTH_LONG).show();
}
};
});
};
}
You could also share a logcat log for better answers but...
After a quick glance I noticed:
final String p1 = pass1.getText().toString();
you don't seem to initialize pass1
anywhere prior to this call.
final String p3 = passwdfile.getString("name uf the variable/key name","default value you need");
getSharedPreferences("here give the name of shared preference",MODE_PRIVATE);
startActivity(new Intent(LogIn.this,Main.class));
Try to remove finish();
Can we get the exception stack so we can find the root cause? My guess is that the app may occurred a null pointer exception, I suggest that to have a null check on p3.
精彩评论