checkbox automatisk inloggning

Diskussion i 'Frågor, support och diskussion' startad av Elprisionero, 26 jan 2011.

  1. Elprisionero

    Elprisionero Baby Droid Medlem

    Blev medlem:
    24 dec 2010
    Inlägg:
    15
    Mottagna gillanden:
    0

    MINA ENHETER

    Hej

    Jag använder mig just nu av en loginsida i min app. det fungerar bra att logga in/registera sig etc.

    Nu skulle jag vilja implementera en checkbox för automatisk inloggning.

    Den metoden jag vill använda mig av är sharedpreferences.




    Kod:
    public void onCreate(Bundle icicle) { 
      super.onCreate(icicle);  
    setContentView(R.layout.login); 
    final EditText usernameTextBox = (EditText) findViewById(R.id.txt_username);  
      final EditText passwordTextBox = (EditText) findViewById(R.id.txt_password);     
      final CheckBox autoLogin = (CheckBox) findViewById(R.id.AutoLogin);
    
      TextView regText = (TextView) findViewById(R.id.register);
      SharedPreferences preferences = getSharedPreferences("company", 0);
      
      
      usernameTextBox.setText(preferences.getString("username", null));
      passwordTextBox.setText(preferences.getString("password", null));
     if (sUserName == null || sPassword == null) {
          //Prompt for username and password
      
      // this is the action listener  
      loginButton.setOnClickListener( new OnClickListener() {   
       public void onClick(View viewParam) { 
        // this gets the resources in the xml file and assigns it to a local variable of type EditText  
    
    
        if (autoLogin.isChecked()) {
         SharedPreferences preferences = getSharedPreferences("company", 0);
         SharedPreferences.Editor editor = preferences.edit(); 
                  editor.putString("username", sUserName);
                  editor.putString("password", sPassword);
                  
                  editor.commit();
                }
     
    Last edited: 26 jan 2011
  2. Kaj

    Kaj Senior Droid Medlem

    Blev medlem:
    12 jun 2009
    Inlägg:
    1 768
    Mottagna gillanden:
    44

    MINA ENHETER

    Vad är din fråga?
     
  3. Elprisionero

    Elprisionero Baby Droid Medlem

    Blev medlem:
    24 dec 2010
    Inlägg:
    15
    Mottagna gillanden:
    0

    MINA ENHETER

    det här är en bit av min kod men när jag försöker logga in får jag bara nullpointerexception i : (autoLogin.isChecked()) {
    SharedPreferences preferences = getSharedPreferences("company", 0);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("username", sUserName);
    editor.putString("password", sPassword);
    editor.commit();
    vad gör jag för fel?
    Kod:
    
    public class Login extends Activity  {
    	private String sUserName ;  
    	private String sPassword ;
    	private Button loginButton;
    	private CheckBox autoLogin;
    
    
    public void onCreate(Bundle icicle) { 
    		super.onCreate(icicle);    
    		// load up the layout  
    		setContentView(R.layout.login);        
    
    TextView regText = (TextView) findViewById(R.id.register);
    		SharedPreferences preferences = getSharedPreferences("company", 0);
    		EditText usernameTextBox = (EditText) findViewById(R.id.txt_username);  
    		EditText passwordTextBox = (EditText) findViewById(R.id.txt_password);     
    		loginButton = (Button)findViewById(R.id.login_button); 
    		autoLogin =((CheckBox)findViewById(R.id.AutoLogin));
    
    
    		usernameTextBox.setText(preferences.getString("username", ""));
    		passwordTextBox.setText(preferences.getString("password", ""));
    
    loginButton.setOnClickListener( new OnClickListener() {  
    
    			public void onClick(View viewParam) { 
    				// this gets the resources in the xml file and assigns it to a local variable of type EditText  
    
    
    				if (autoLogin.isChecked()) {
    					SharedPreferences preferences = getSharedPreferences("company", 0);
    					SharedPreferences.Editor editor = preferences.edit();	
    					editor.putString("username", sUserName);
    					editor.putString("password", sPassword);
    					editor.commit();
    
    
    				}
     
  4. ozp

    ozp Teen Droid Medlem

    Blev medlem:
    6 maj 2010
    Inlägg:
    250
    Mottagna gillanden:
    31

    MINA ENHETER

    R.id.AutoLogin finns inte med i din layout