我正在为我的Android应用程序创建一个注册/登录页面。这是我的代码。我使用了共享偏好。代码没有错误。但是每当我在eclipse中运行它时,只要我点击注册按钮就会冻结,并且发生错误"确认透视切换"
enter code here
MainActivity.java
package com.example.login;
public class MainActivity extends Activity {
EditText pass;
Button login,signup;
SharedPreferences sharedpreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pass=(EditText)findViewById(R.id.editText1);
login=(Button)findViewById(R.id.button1);
signup=(Button)findViewById(R.id.button2);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
sharedpreference= getSharedPreferences("MyPREFERENCES", MODE_PRIVATE);
String restoredText = sharedpreference.getString("Name", "password");
if(pass.getText().toString().equals(restoredText)){
Toast toast = Toast.makeText(getApplicationContext(), "success",Toast.LENGTH_LONG);
toast.show();
Intent intent = new Intent(MainActivity.this, JournalActivity.class);
startActivity(intent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(), "INVALID PASSWORD",Toast.LENGTH_LONG);
toast.show();
}
}
});
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sign= new Intent(MainActivity.this,SignupActivity.class);
startActivity(sign);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
SignupActivity.java
package com.example.login;
public class SignupActivity extends Activity
{
EditText editTextUserName,editTextPassword,editTextConfirmPassword;
Button btnCreateAccount;
SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String password=editTextPassword.getText().toString();
String confirmPassword=editTextConfirmPassword.getText().toString();
// check if any of the fields are vaccant
if(password.equals("")||confirmPassword.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword))
{
Toast.makeText(getApplicationContext(), "Password Does Not Matches", Toast.LENGTH_LONG).show();
return;
}
else
{
pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor= pref.edit();
editor.putString("Name", password);
editor.apply();
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
Intent i= new Intent(SignupActivity.this, MainActivity.class);
startActivity(i);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}