Here is a code for one of my Android studio java classes:
Button button1; //log in
Button button2; //sign up
public View.OnClickListener button1Listener = new View.OnClickListener() {
public void onClick (View view){
Intent loginActivity = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(loginActivity);
}};
public View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick (View view){
Intent signupActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(signupActivity);
}};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_page);
button1 = (Button) findViewById(R.id.button3);
button2 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(button1Listener);
button2.setOnClickListener(button2Listener);
}
Main Activity:
public class MainActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
Button button, button2;
public FirebaseAuth auth;
public FirebaseAuth.AuthStateListener authlistener;
String email, pass;
ProgressBar bar;
public View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick (View view){
email = editText.getText().toString().trim();
pass = editText2.getText().toString().trim();
createUser();
}};
public View.OnClickListener backButtonListener = new View.OnClickListener() {
public void onClick (View view){
Intent backtoMain = new Intent(getApplicationContext(), FrontPageActivity.class);
startActivity(backtoMain);
}};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
editText2 = (EditText)findViewById(R.id.editText2);
button = (Button)findViewById(R.id.button2);
auth = FirebaseAuth.getInstance();
button.setOnClickListener(buttonListener);
bar = (ProgressBar)findViewById(R.id.progressBar2);
bar.setVisibility(View.INVISIBLE);
button2 = (Button)findViewById(R.id.button5);
button2.setOnClickListener(backButtonListener);
}
public void createUser(){
bar.setVisibility(View.VISIBLE);
auth.createUserWithEmailAndPassword(email, pass)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
bar.setVisibility(View.INVISIBLE);
} else{
Toast.makeText(getApplicationContext(), "Failed, Password must be >=6 characters", Toast.LENGTH_LONG).show();
bar.setVisibility(View.INVISIBLE);
}
}
});
}
}
Login Activity:
public class LoginActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
Button button;
public FirebaseAuth Auth = FirebaseAuth.getInstance();
public FirebaseAuth.AuthStateListener authlistener;
String email, pass;
ProgressBar bar;
public View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick (View view){
email = editText.getText().toString().trim();
pass = editText2.getText().toString().trim();
logInUser();
}};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
editText2 = (EditText)findViewById(R.id.editText2);
button = (Button)findViewById(R.id.button2);
Auth = FirebaseAuth.getInstance();
button.setOnClickListener(buttonListener);
bar = (ProgressBar)findViewById(R.id.progressBar2);
bar.setVisibility(View.INVISIBLE);
authlistener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
} else {
// User is signed out
}
// ...
}
};
// ...
}
#Override
public void onStart() {
super.onStart();
Auth.addAuthStateListener(authlistener);
}
#Override
public void onStop() {
super.onStop();
if (authlistener != null) {
Auth.removeAuthStateListener(authlistener);
}
}
public void logInUser(){
bar.setVisibility(View.VISIBLE);
Auth.signInWithEmailAndPassword(email, pass)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Intent loggedInActivity = new Intent(getApplicationContext(), Loggedin.class);
startActivity(loggedInActivity);
}
else{
Toast.makeText(getApplicationContext(), "There was an error, try again", Toast.LENGTH_SHORT).show();
}
}
});
}
}
So, I have 2 buttons, "login," and "sign-up" and both should lead to a different intent. The sign-up leads to the mainactivity.class which is the sign up page basically.
However, even though I have mapped button1 to the loginAcitivty class, it still goes to MainActivity.class. Why is this happening?
Check your LoginActivity in this you have set setContentView(R.layout.activity_main); it is inflating the content layout of activity_main
The layout you are inflating in both the activities are same..
change your code like this,
Button button1; //log in
Button button2; //sign up
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent loginActivity = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(loginActivity);
}};
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent signupActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(signupActivity);
}};
Related
I don't know why I am getting error on login and sign up activity with firebase.
Problem what I am facing is - whenever i click on signup button or in login button its start crashing sometimes and sometimes its redirect me to Mainactivity.class.
my Signupactivity.class
public class SignUpActivity extends AppCompatActivity {
ActivitySignUpBinding binding;
private FirebaseAuth auth;
FirebaseDatabase database;
ProgressDialog progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivitySignUpBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().hide();
auth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();
progress = new ProgressDialog(this);
progress.setTitle("Creating Account");
progress.setMessage("We're Creating your Account");
binding.btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
progress.show();
auth.createUserWithEmailAndPassword
(binding.edEmail.getText().toString() , binding.edPassword.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
userModels user = new userModels
(binding.edUsername.getText().toString() , binding.edEmail.getText().toString() , binding.edPassword.getText().toString());
String id = task.getResult().getUser().getUid();
database.getReference().child("Users").child(id).setValue(user);
progress.dismiss();
if (task.isSuccessful()){
Intent intent = new Intent(SignUpActivity.this , LoginActivty.class);
startActivity(intent);
}
else {
Toast.makeText(SignUpActivity.this, "Authentication faild", Toast.LENGTH_SHORT).show();
}
}
});
}
});
binding.tvRegister.setOnClickListener(view -> {
Intent intent = new Intent(SignUpActivity.this , LoginActivty.class);
startActivity(intent);
});
}
}
My problem is the title
As soon as I have done Login activity, it skips my Login activity, it shows the main activity without sign in.
Before i code in Login activity, it ran normally, meaning that the Login activity would show 1st
Here my code
MainActivity
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private ViewPager viewPager;
private TabLayout tabLayout;
private TabsAccessorAdapter tabsAccessorAdapter;
private FirebaseUser currentUser;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth=FirebaseAuth.getInstance();
currentUser=mAuth.getCurrentUser();
mToolbar= findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("F9");
viewPager= findViewById(R.id.main_tabs_paper);
tabsAccessorAdapter= new TabsAccessorAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabsAccessorAdapter);
tabLayout= findViewById(R.id.main_tabs);
tabLayout.setupWithViewPager(viewPager);
}
#Override
protected void onStart() {
super.onStart();
if(currentUser==null)
{
SendUserToLoginActivity();
}
}
private void SendUserToLoginActivity() {
Intent loginIntent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(loginIntent);
}
}
LoginActivity
public class LoginActivity extends AppCompatActivity {
private FirebaseUser currentUser;
private Button LoginButton,PhoneLoginButton;
private TextInputLayout UserEmail,UserPassword;
private Button CreateNewAccount ;
private TextView ForgetPassword;
private FirebaseAuth mAuth;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
currentUser=mAuth.getCurrentUser();
InitializeFields();
CreateNewAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SendUserToRegisterActivity();
}
});
LoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AllowUserToLogin();
}
});
}
private void AllowUserToLogin() {
String email=UserEmail.getEditText().getText().toString();
String password=UserPassword.getEditText().getText().toString();
if(email.isEmpty()||!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()){
Toast.makeText(this,"Enter a valid email address...",Toast.LENGTH_SHORT).show();
}
if(password.isEmpty() || password.length() < 5){
Toast.makeText(this,"Password should be at least 6 character",Toast.LENGTH_SHORT).show();
}
if(password.isEmpty()&&email.isEmpty()){
Toast.makeText(this,"Please enter your email and password",Toast.LENGTH_SHORT).show();
}
else{
progressDialog.setMessage("Loading...");
progressDialog.setCanceledOnTouchOutside(true);
progressDialog.show();
mAuth.signInWithEmailAndPassword(email,password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
SendUserToMainActivity();
Toast.makeText(LoginActivity.this, "Successful!!", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
else
{
String message = task.getException().toString();
Toast.makeText(LoginActivity.this,"Error: "+ message,Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}
}
private void InitializeFields() {
LoginButton =(Button) findViewById(R.id.buttonLogin);
PhoneLoginButton =(Button) findViewById(R.id.buttonLoginByPhone);
UserEmail = (TextInputLayout) findViewById(R.id.emailLogin);
UserPassword = (TextInputLayout) findViewById(R.id.passwordLogin);
CreateNewAccount=(Button) findViewById(R.id.buttonRegister);
ForgetPassword=(TextView) findViewById(R.id.forgetpasswordLogin);
progressDialog = new ProgressDialog(this);
}
/////Neu da dang nhap se chuyen den trang mainactivity
#Override
protected void onStart() {
super.onStart();
if(currentUser !=null)
{
SendUserToMainActivity();
}
}
private void SendUserToMainActivity() {
Intent loginIntent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(loginIntent);
}
private void SendUserToRegisterActivity() {
Intent registerIntent = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(registerIntent);
}
}
RegisterActivity
public class RegisterActivity extends AppCompatActivity {
private Button CreateButton,AlreadyHaveAccount;
private TextInputLayout UserEmail,UserPassword;
private FirebaseAuth mAuth;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth=FirebaseAuth.getInstance();
InitializeFields();
AlreadyHaveAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SendUserToLoginActivity();
}
});
CreateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CreateNewAccount();
}
});
}
private void CreateNewAccount() {
String email=UserEmail.getEditText().getText().toString();
String password=UserPassword.getEditText().getText().toString();
if(email.isEmpty()||!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()){
Toast.makeText(this,"Enter a valid email address...",Toast.LENGTH_SHORT).show();
}
if(password.isEmpty() || password.length() < 5){
Toast.makeText(this,"Password should be at least 6 character",Toast.LENGTH_SHORT).show();
}
if(password.isEmpty()&&email.isEmpty()){
Toast.makeText(this,"Please enter your email and password",Toast.LENGTH_SHORT).show();
}
else{
progressDialog.setMessage("Creating.....");
progressDialog.setCanceledOnTouchOutside(true);
progressDialog.show();
//Ket noi FireBase tao email mat khau
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful())
{
SendUserToLoginActivity();
Toast.makeText(RegisterActivity.this,"Account Created Successfully...",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
else
{
String message = task.getException().toString();
Toast.makeText(RegisterActivity.this,"Error: "+ message,Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}
}
private void InitializeFields() {
CreateButton =(Button) findViewById(R.id.buttonRegister);
UserEmail = (TextInputLayout) findViewById(R.id.emailRegister);
UserPassword = (TextInputLayout) findViewById(R.id.passwordRegister);
AlreadyHaveAccount=(Button) findViewById(R.id.buttonHaveAccount);
progressDialog=new ProgressDialog(this);
}
private void SendUserToLoginActivity() {
Intent loginIntent = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(loginIntent);
}
}
I think the problem is in the LoginActivity, because when I wrote some code in it, the problem arose
Once the user is logged in, onStart will send the user to mainActivity
You need to create a logOut button in MainActivity, So it will not skip login page
you can also clear data of the app from the devices to make the currentUser == null
for logout
FirebaseAuth auth = FirebaseAuth.getInstance()
auth.logOut();
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
i want to register a new user using firebaseAuth but i keep getting error
i have debugged using invalidate and restart and rebuilt the project, and even clean project yet it won't run successfully
RegisterActivity
public class RegisterActivity extends AppCompatActivity{
private EditText mNickname;
private EditText mEmail;
private EditText mPassword;
private TextView gotoLoginPage;
private Button mRegisterbutton;
private FirebaseAuth mAuth;
private ProgressDialog mRegProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mNickname = findViewById(R.id.registerNicknameId);
mEmail = findViewById(R.id.registerEmailId);
mPassword = findViewById(R.id.registerPasswordId);
FirebaseApp.initializeApp(this);
mRegisterbutton = findViewById(R.id.registerButton);
gotoLoginPage = findViewById(R.id.gotoLoginPage);
getSupportActionBar().setTitle("Create new account");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mRegProgress = new ProgressDialog(this);
mRegisterbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String nickname = mNickname.getText().toString();
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
if (mNickname.equals("")|| mEmail.equals("") || mPassword.equals("")){
Toast.makeText(getApplicationContext(), "Fill in the fields", Toast.LENGTH_LONG).show();
mRegProgress.show();
registerUser(email, password);
}
Intent toHomePage = new Intent(RegisterActivity.this, HomeActivity.class);
startActivity(toHomePage);
finish();
}
});
gotoLoginPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tologinPage = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(tologinPage);
}
});
}
private void registerUser(String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Intent gotoHomePage = new Intent(RegisterActivity.this, HomeActivity.class);
startActivity(gotoHomePage);
finish();
}
}
});
}
}
MainActivity
public class HomeActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
FirebaseApp.initializeApp(this);
mToolbar =findViewById(R.id.toolbarId);
getSupportActionBar().setTitle("PhorumChat");
}
private void sendToStart() {
Intent goToWelcomePage = new Intent(HomeActivity.this, WelcomeActivity.class);
startActivity(goToWelcomePage);
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getItemId() == R.id.main_logoutBtn){
FirebaseAuth.getInstance().signOut();
sendToStart();
}
return true;
}
#Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
private void updateUI(FirebaseUser currentUser) {
if (currentUser ==null){
Intent homepage= new Intent(HomeActivity.this, WelcomeActivity.class);
startActivity(homepage);
finish();
}
}
}
This is the error i got when i run the program. Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object refererence
The error comes from:
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
Since you're not initializing mAuth anywhere, this is null.getCurrentUser(), which throws the error you get.
Either initialize mAuth in onCreate:
mAuth = FirebaseAuth.getInstance();
Or just replace mAuth with FirebaseAuth.getInstance() where you use it.
I'm using the following class and ive been trying for the last 24 hours i cant figure out why its not working. when i press the button it does nothing.
I'm creating this so i can verify the use login information
My class
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG);
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
});
}}
I Added my XML file on https://codeshare.io/5XWxOE
You are creating Toast but do not call it to show its content try code blew :
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG).show();
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
you forgot to use show()
Please try this solution
Toast.makeText(login.this,"it
clicked",Toast.LENGTH_LONG).show;
Instead of the getApplicationContext() you have.
Try this ...
TextView mt;
Button clickButton;
mt=(TextView) findViewById(R.id.messy);
clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "Button click toast", Toast.LENGTH_SHORT).show();
mt.setText("Your text here");
}
});
When something is going wierd.. you can check basic log. try this one~
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setText("Button Found");
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
clickButton.setText("Button Clicked");
}
});
}}
There is another way to write code for button clicking
public class login extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button firstButton = (Button) findViewById(R.id.loginButton);
firstButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginButton: {
Toast.makeText(login.this, "Button click toast", Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.messy);
textView.setText("Your text here");
break;
}
default: {
Toast.makeText(login.this, "Something happens", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
I hope this will resolve your issue.
A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
I am trying to log in and sign up using my application and found the above error.
Following is my code of main activity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
Intent intent = new Intent(MainActivity.this, LoginSignupActivity.class);
startActivity(intent);
finish();
} else {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
Intent intent = new Intent(MainActivity.this, Welcome.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(MainActivity.this, LoginSignupActivity.class);
startActivity(intent);
finish();
}
}
}
}
Following is my code of LoginSigupActivity
public class LoginSignupActivity extends ActionBarActivity {
Button loginButton;
Button signupButton;
#Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_login_signup);
loginButton = (Button) findViewById(R.id.btn_launcherSignIn);
signupButton = (Button) findViewById(R.id.btn_launcherSignUp);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(LoginSignupActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
signupButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(LoginSignupActivity.this, SignUpActivity.class);
startActivity(intent);
finish();
}
});
}
}
Folloing is my login activity class code
public class LoginActivity extends ActionBarActivity {
Button loginButton;
String txtUsername;
String txtPassword;
EditText password;
EditText username;
#Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_login);
loginButton = (Button) findViewById(R.id.login);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtUsername = username.getText().toString();
txtPassword = password.getText().toString();
ParseUser.logInInBackground(txtUsername, txtPassword, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (user != null) {
Intent intent = new Intent(LoginActivity.this, Welcome.class);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "This user does not exist! Please Sign up", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
Following is my signup activity code
public class SignUpActivity extends ActionBarActivity {
Button signupButton;
String txtUsername;
String txtPassword;
EditText password;
EditText username;
#Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_signup);
signupButton = (Button) findViewById(R.id.signup);
signupButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtUsername = username.getText().toString();
txtPassword = password.getText().toString();
if(txtUsername.equals("") && txtPassword.equals("")){
Toast.makeText(getApplicationContext(), "Please complete the signup form", Toast.LENGTH_SHORT).show();
}else{
ParseUser user = new ParseUser();
user.setUsername(txtUsername);
user.setPassword(txtPassword);
user.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
if(e==null){
Toast.makeText(getApplicationContext(), "Signed up successfully...!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Signup Error", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
}
And following is my Parse connector class code
public class ParseConnecter extends MultiDexApplication{
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "zblr0TmjldGaV2xSl5******aFhe6DW78FjA2u", "9Ys6j7uY68115********2pvo5Ldfia");
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}