开发者

SharedPreferences class

开发者 https://www.devze.com 2023-02-06 22:00 出处:网络
I\'m creating an app which ask the first time user to input a password. However whenever I press the Submit button the application stops and I think it\'s because of the class I made for he SharedPref

I'm creating an app which ask the first time user to input a password. However whenever I press the Submit button the application stops and I think it's because of the class I made for he SharedPreferences. I don't know what to put in the class. Need some help please. Thanks

package com.android.steg;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Password extends Activity implements OnClickListener
{
Button submitButton;
EditText passwordEditText;
private SharedPreferences SharedPreference;
public static final String PREFS_PRIVATE = "PREFS_PRIVATE";
public static final String KEY_PRIVATE = "KEY_PRIVATE";
public static final String PREFS_READ = "PREFS_READ";
public static final String KEY_READ = "KEY_READ";
public static final String PREFS_WRITE = "PREFS_WRITE";
public static final String KEY_WRITE = "KEY_WRITE";
public static final String PREFS_READ_开发者_如何学JAVAWRITE = "PREFS_READ_WRITE";
public static final String KEY_READ_WRITE = "KEY_READ_WRITE";



public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pass);
    Button submitButton = (Button) findViewById(R.id.submitButton);
    submitButton.setOnClickListener(this);
}

public void onClick(View v)
{ 
    EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);
    SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE);
    String password = prefs.getString("password","");
    if(password=="")
    {
        Editor edit = prefs.edit();
        edit.putString("password",passwordEditText.getText().toString());
        edit.commit();
        StartMain();
    }
    else
    {
        if(passwordEditText.getText().toString()== password)
        {
             StartMain();
        }
    }

}

public void StartMain()
{
     Intent intent = new Intent(this, MainActivity.class);
     startActivity(intent);
}
 }

Whenever I press the submit button, it won't send me to the start the MainActivity class.


You are not comparing Strings correctly. Replace

if(password=="")

with

if("".equals(password))

and

if(passwordEditText.getText().toString()== password)

with

if(passwordEditText.getText().toString().equals(password))

EDIT
The reason you need to make these changes is that (password=="") will always be false, so the else block will always execute. Within the else block, the only test is (passwordEditText.getText().toString()== password) and this will also always be false. As such, the two calls to StartMain() are both within unreachable code blocks and are never executed. You can easily demonstrate this by putting a bunch of Log.d("MY_TAG","message goes here"); lines in the various if and else blocks.

EDIT 2
Ah. Woops. There is an additional issue with the code that I was blinded to by the obviousness of the String comparison issue. You are confusing the scope of your button declaration. You first define it for the class:

Button submitButton;

The you redefine it within onCreate():

Button submitButton = (Button) findViewById(R.id.submitButton);

Change this last line to:

submitButton = (Button) findViewById(R.id.submitButton);
0

精彩评论

暂无评论...
验证码 换一张
取 消