简体   繁体   English

SharedPreferences类

[英]SharedPreferences class

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. 但是,每当我按下Submit按钮时,应用程序就会停止,我认为这是因为我为他的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_WRITE = "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. 每当我按下“提交”按钮时,它都不会将我引导到MainActivity类的开始。

You are not comparing Strings correctly. 您没有正确比较Strings 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. 您需要进行这些更改的原因是(password=="")始终为false ,因此else块将始终执行。 Within the else block, the only test is (passwordEditText.getText().toString()== password) and this will also always be false . else块中,唯一的测试是(passwordEditText.getText().toString()== password)并且该值始终为false As such, the two calls to StartMain() are both within unreachable code blocks and are never executed. 这样,对StartMain()的两次调用都在无法访问的代码块内,并且从不执​​行。 You can easily demonstrate this by putting a bunch of Log.d("MY_TAG","message goes here"); 您可以通过放置一堆Log.d("MY_TAG","message goes here");来轻松演示这一点Log.d("MY_TAG","message goes here"); lines in the various if and else blocks. 各种ifelse块中的行。

EDIT 2 编辑2
Ah. 啊。 Woops. 哎呀 There is an additional issue with the code that I was blinded to by the obviousness of the String comparison issue. String比较问题的明显性使我看不见代码的另一个问题。 You are confusing the scope of your button declaration. 您混淆了按钮声明的范围。 You first define it for the class: 首先为类定义它:

Button submitButton;

The you redefine it within onCreate() : 您在onCreate()重新定义了它:

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

Change this last line to: 将最后一行更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM