How can I push chair data inside parent table? - java

Here is my class code, my problem is that I do not know where to place edtChair in order to push its data under the parent table?
I tried adding edtChair in this line of code ( Table table = new Table(edtPassword.getText().toString) by placing it beside edtpassword and adding comma.Still does not work at all. How should I construct the code?
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.alber.tbmcmapp.Model.Chairs;
import com.example.alber.tbmcmapp.Model.Table;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rengwuxian.materialedittext.MaterialEditText;
public class SignUp extends AppCompatActivity {
MaterialEditText edtTable, edtPassword,edtChair;
Button btnSignUp;
//DatabaseReference cDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
edtTable = (MaterialEditText)findViewById(R.id.edtTable);
edtChair = (MaterialEditText)findViewById(R.id.edtChair);
edtPassword = (MaterialEditText)findViewById(R.id.edtPassword);
btnSignUp = (Button)findViewById(R.id.btnSignUp);
//Firebase
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference table_user = database.getReference("Table");
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final ProgressDialog mDialog = new ProgressDialog(SignUp.this);
mDialog.setMessage("Please wait...");
mDialog.show();
table_user.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Check if already exist
if(dataSnapshot.child(edtTable.getText().toString()).exists())
{
mDialog.dismiss();
Toast.makeText(SignUp.this, "Table already registered", Toast.LENGTH_SHORT).show();
}
else
{
mDialog.dismiss();
Table table = new Table(edtPassword.getText().toString());
table_user.child(edtTable.getText().toString()).setValue(table);
Toast.makeText(SignUp.this, " Table Created ", Toast.LENGTH_SHORT).show();
finish();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
}

Related

My Quiz App is crashing when I load Categories from Firebase Firestore but able to recieve users data to firestore from App

When I try to load categories in any activity the quizapp is getting crashed when i load. But when i send users data to firestore, its working.
All the data set in DbQuery.java matches the names entered with Firestore data. But There is something wrong in this that whenever I load this data in login Activity, signup activity or splash activity the app is getting crashed.
Here is my code:
DbQuery.java
package com.nightout.ttsgeneralawareness;
import android.content.Intent;
import android.util.ArrayMap;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.firestore.WriteBatch;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class DbQuery {
public static FirebaseFirestore g_firestore;
public static List<CategoryModel> g_catList = new ArrayList<>();
public static void createUserData(String email, String name, MyCompleteListener completeListener) {
Map<String, Object> userData = new ArrayMap<>();
userData.put("EMAIL_ID", email);
userData.put("NAME", name);
userData.put("TOTAL_SCORE", 0);
DocumentReference userDoc = g_firestore.collection("USERS")
.document(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid());
WriteBatch batch = g_firestore.batch();
batch.set(userDoc, userData);
DocumentReference countDoc = g_firestore.collection("USERS").document("TOTAL_USERS");
batch.update(countDoc, "COUNT", FieldValue.increment(1));
batch.commit()
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void unused) {
completeListener.onSuccess();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
completeListener.onFailure();
}
});
}
public static void loadCategories(MyCompleteListener completeListener) {
g_catList.clear();
g_firestore.collection("QUIZ").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
Map<String, QueryDocumentSnapshot> docList = new ArrayMap<>();
for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
docList.put(doc.getId(), doc);
}
QueryDocumentSnapshot catListDooc = docList.get("Categories");
long catCount = catListDooc.getLong("COUNT");
for (int i = 1; i <= catCount; i++) {
String catID = catListDooc.getString("CAT" + String.valueOf(i) + "_ID");
QueryDocumentSnapshot catDoc = docList.get(catID);
int noOfTest = catDoc.getLong("NO_OF_TESTS").intValue();
String catName = catDoc.getString("NAME");
g_catList.add(new CategoryModel(catID, catName, noOfTest));
}
completeListener.onSuccess();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
completeListener.onFailure();
}
});
}
LoginActivity.java
package com.nightout.ttsgeneralawareness;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import java.util.Locale;
public class LoginActivity extends AppCompatActivity {
private EditText email, password;
private Button loginButton;
private TextView forgotpwd, signup_button;
private FirebaseAuth mAuth;
private Dialog progressDialog;
private TextView dialogText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().hide();
email = findViewById(R.id.email);
password = findViewById(R.id.password);
loginButton = findViewById(R.id.login_button);
forgotpwd = findViewById(R.id.forgot_pwd);
signup_button = findViewById(R.id.signup_button);
//google
progressDialog = new Dialog(LoginActivity.this);
progressDialog.setContentView(R.layout.dialog);
progressDialog.setCancelable(false);
progressDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
dialogText = progressDialog.findViewById(R.id.dialog_text);
dialogText.setText("Signing in...");
mAuth = FirebaseAuth.getInstance();
//google
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (validData())
{
login();
}
}
});
signup_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(intent);
}
});
}
private boolean validData()
{
if (email.getText().toString().isEmpty())
{
email.setError("Enter E-Mail ID");
return false;
}
if (password.getText().toString().isEmpty())
{
password.setError("Enter Password");
return false;
}
return true;
}
private void login()
{
progressDialog.show();
mAuth.signInWithEmailAndPassword(email.getText().toString().trim(), password.getText().toString().trim())
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Toast.makeText(LoginActivity.this,"Login Success",Toast.LENGTH_SHORT).show();
DbQuery.loadCategories(new MyCompleteListener() {
#Override
public void onSuccess() {
progressDialog.dismiss();
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}
#Override
public void onFailure() {
progressDialog.dismiss();
Toast.makeText(LoginActivity.this,"Something Went Wrong!",Toast.LENGTH_SHORT).show();
}
});
} else {
progressDialog.dismiss();
// If sign in fails, display a message to the user.
Toast.makeText(LoginActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
}

I am having problem uploading user data on firestore during time of registration using firebase authentication

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class Register extends AppCompatActivity {
Button btn2_signup;
Button btn_login;
EditText e_mail, pass_word,phone_number;
public EditText user_name;
FirebaseAuth mAuth;
FirebaseFirestore fStore;
public static String username;
String userID;
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
e_mail=findViewById(R.id.email);
pass_word=findViewById(R.id.password1);
user_name=findViewById(R.id.username);
phone_number=findViewById(R.id.phone_number);
btn2_signup=findViewById(R.id.sign);
btn_login = findViewById(R.id.login);
mAuth=FirebaseAuth.getInstance();
btn2_signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = e_mail.getText().toString().trim();
String password= pass_word.getText().toString().trim();
if(email.isEmpty())
{
e_mail.setError("Email is empty");
e_mail.requestFocus();
return;
}
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
{
e_mail.setError("Enter the valid email address");
e_mail.requestFocus();
return;
}
if(password.isEmpty())
{
pass_word.setError("Enter the password");
pass_word.requestFocus();
return;
}
if(password.length()<6)
{
pass_word.setError("Length of the password should be more than 6");
pass_word.requestFocus();
return;
}
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
username = user_name.getText().toString();
Toast.makeText(Register.this,"You are successfully Registered", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Register.this,Login.class);
startActivity(i);
}
else
{
Toast.makeText(Register.this,"You are not Registered! Try again",Toast.LENGTH_SHORT).show();
}
}
});
}
});
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Register.this,Login.class);
startActivity(i);
}
});
}
public void uploadUserData(){
String email = e_mail.getText().toString();
String phoneNumber = phone_number.getText().toString();
userID = mAuth.getCurrentUser().getUid();
CollectionReference collectionReference = fStore.collection("Users data");
Map<String,Object> user = new HashMap<>();
user.put("timestamp", FieldValue.serverTimestamp());
user.put("userid",userID);
user.put("email",email);
user.put("phoneNumber",phoneNumber);
collectionReference.add(user)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Error!",Toast.LENGTH_SHORT).show();
//Log.w(TAG, "Error!", e);
}
});
}
}
I want to upload the user data on firestore during the time of registration of user using firebase authentication. I have created a method called uploadUserData() but when its called from createUserWithEmailAndPassword in onComplete method the app stops. Can someone please point out the errors in code and the changes required.

An intent redirects me to a wrong activity

Unfortunately the button I tried to design doesn't work. I have three activities: Login, Register and MainActivity (which is a blank activity) and on the Login activity I have this register button ("button") that I want to redirect to Register activity but it redirects me to a blank page when I run the app. What should I do?
package com.example.scooterzapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class Register extends AppCompatActivity {
FirebaseAuth fAuth;
EditText etEmail,etParola,etNume,etPrenume,etUsername,etVarsta,etNumarDeTelefon;
Button bReg;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etNume = findViewById(R.id.etNume);
etPrenume = findViewById(R.id.etPrenume);
etUsername = findViewById(R.id.etUsername);
etParola = findViewById(R.id.etParola);
etEmail = findViewById(R.id.etEmail);
etNumarDeTelefon = findViewById(R.id.etNumarDeTelefon);
etVarsta = findViewById(R.id.etVarsta);
bReg= findViewById(R.id.bReg);
fAuth=FirebaseAuth.getInstance();
progressBar=findViewById(R.id.progressBar);
if(fAuth.getCurrentUser()!= null){
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
bReg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String Email = etEmail.getText().toString().trim();
String Parola = etParola.getText().toString().trim();
if (TextUtils.isEmpty(Email)) {
etEmail.setError("Campul trebuie completat.");
return;
}
if (TextUtils.isEmpty(Parola)) {
etParola.setError("Campul trebuie completat.");
return;
}
if (Parola.length() < 6) {
etParola.setError("Parola trebuie sa fie formata din cel putin 6 caractere");
return;
}
progressBar.setVisibility(View.VISIBLE);
fAuth.createUserWithEmailAndPassword(Email,Parola).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(Register.this, "V-ati inregistrat cu succes!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Login.class));
}else{
Toast.makeText(Register.this, "Inregistrare esuata.", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
Greet from Nice ;) try below code
button.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,Register.class));
}
});

The password is invalid or the user does not have a password

I'm trying to create a login and register system in Android Studio using Firebase. Registration works pretty well, user is created in Firebase successfully. But whenever I try to login it gives me this error "The password is invalid or the user does not have a password" even though the password I entered is the same I registered with. I think the problem might be with my code because user can login perfectly when I create account for given user on Firebase Console directly. What might cause this problem?
I provided my login and my register activity code:
Login activity
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity
{
private EditText emailLoginEditText, passwordLoginEditText;
private Button loginBtn;
private TextView signupLink, forgetPassword;
private ProgressDialog progressDialog;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailLoginEditText = findViewById(R.id.email_login);
passwordLoginEditText = findViewById(R.id.password_login);
loginBtn = findViewById(R.id.btn_login);
signupLink = findViewById(R.id.signup_reg);
forgetPassword = findViewById(R.id.forget_password);
progressDialog = new ProgressDialog(this);
mAuth = FirebaseAuth.getInstance();
signupLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this, RegistrationActivity.class));
}
});
forgetPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this, ResetActivity.class));
}
});
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
loginDetails();
}
});
}
private void loginDetails()
{
String email = emailLoginEditText.getText().toString().trim();
String password = passwordLoginEditText.getText().toString();
if(TextUtils.isEmpty(email))
{
emailLoginEditText.setError("Email can't be empty...");
return;
}
if(TextUtils.isEmpty(password))
{
passwordLoginEditText.setError("Password can't be empty...");
return;
}
else
{
progressDialog.setTitle("Login");
progressDialog.setMessage("Please wait while we are connecting you");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task)
{
if(task.isSuccessful())
{
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
else
{
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
And this is RegistrationActivity:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class RegistrationActivity extends AppCompatActivity
{
private EditText mEmailEditText, mPasswordEditText;
private Button mRegisterBtn;
private TextView SigninLink;
private ProgressDialog progressDialog;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
mEmailEditText = findViewById(R.id.email_reg);
mPasswordEditText = findViewById(R.id.password_reg);
mRegisterBtn = findViewById(R.id.btn_reg);
SigninLink = findViewById(R.id.signin_here);
progressDialog = new ProgressDialog(this);
mAuth = FirebaseAuth.getInstance();
SigninLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
startActivity(new Intent(RegistrationActivity.this, MainActivity.class));
}
});
registration();
}
private void registration()
{
mRegisterBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
String email = mEmailEditText.getText().toString().trim();
String password = mEmailEditText.getText().toString();
if(TextUtils.isEmpty(email))
{
mEmailEditText.setError("Email can't be empty...");
return;
}
if(TextUtils.isEmpty(password))
{
mPasswordEditText.setError("Password can't be empty...");
return;
}
progressDialog.setTitle("Creating Account");
progressDialog.setMessage("Please wait while we are creating your account");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task)
{
if(task.isSuccessful())
{
progressDialog.dismiss();
startActivity(new Intent(RegistrationActivity.this, HomeActivity.class));
}
else
{
Toast.makeText(RegistrationActivity.this, "Registration failed...", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}
});
}
}

Getting "java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()" in Firebase

Image of DB in firebase console
I don't really know what's wrong here, I am able to add data on the other activity. I am getting error "java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()" and don't know how to fix. There are 3 other classes created only one other is adding data to firebase realtime database storage.
package com.example.akshay.katiroll.FirstScreen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.akshay.katiroll.R;
import com.example.akshay.katiroll.SecondScreen.welcome;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class Profile extends AppCompatActivity {
EditText fName, lName, PhNo, BDay, Zip;
Button svBtn;
DatabaseReference mDataReference;
String keyUser;
String fNameStr, lNameStr, PhNoStr, BDayStr, ZipStr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
keyUser = getIntent().getStringExtra("USER_KEY");
mDataReference = FirebaseDatabase.getInstance().getReference().child("Users").child(keyUser);//error in this line
fName = (EditText) findViewById(R.id.fName);
lName = (EditText) findViewById(R.id.lName);
PhNo = (EditText) findViewById(R.id.phno);
BDay = (EditText) findViewById(R.id.bday);
Zip = (EditText) findViewById(R.id.zip);
svBtn = (Button) findViewById(R.id.svBtn);
svBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fNameStr = fName.getText().toString();
lNameStr = lName.getText().toString();
PhNoStr = PhNo.getText().toString();
BDayStr = BDay.getText().toString();
ZipStr = Zip.getText().toString();
if(!TextUtils.isEmpty(fNameStr) && !TextUtils.isEmpty(lNameStr) && !TextUtils.isEmpty(PhNoStr)) {
DatabaseReference mDataRef = mDataReference.child("Users").push();
mDataRef.child("firstName").setValue(""+fNameStr);
mDataRef.child("lastName").setValue(""+lNameStr);
mDataRef.child("phoneNumber").setValue(""+PhNoStr);
mDataRef.child("isVerified").setValue("verified");
if(!TextUtils.isEmpty(BDayStr)){
mDataRef.child("birthday").setValue(""+BDayStr);
}else{
mDataRef.child("birthday").setValue("null");
}
if(!TextUtils.isEmpty(ZipStr)){
mDataRef.child("zipcode").setValue("ZipStr");
}else{
mDataRef.child("zipcode").setValue("null");
}
Toast.makeText(Profile.this, "User profile added", Toast.LENGTH_LONG).show();
startActivity(new Intent(Profile.this, welcome.class));
}else{
Toast.makeText(Profile.this, "Failed to create User Account", Toast.LENGTH_LONG).show();
}
}
});
}
}
package com.example.akshay.katiroll.FirstScreen;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.akshay.katiroll.R;
import com.example.akshay.katiroll.SecondScreen.welcome;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.Iterator;
public class katiRoll extends AppCompatActivity {
EditText userEdit;
EditText passEdit;
String sUser, sPass;
Button sign;
Button reg;
Boolean signInVLogIn;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
DatabaseReference mDatabaseRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kati_roll);
float Re = 255;
float Gr = 148;
float Bl = 0;
userEdit = (EditText)findViewById(R.id.user);
passEdit = (EditText)findViewById(R.id.pass);
sign = (Button) findViewById(R.id.sign);
reg = (Button) findViewById(R.id.reg);
userEdit.setTextColor(Color.rgb(Re,Gr,Bl));
passEdit.setTextColor(Color.rgb(Re,Gr,Bl));
sign.setTextColor(Color.rgb(Re,Gr,Bl));
reg.setTextColor(Color.rgb(Re,Gr,Bl));
mAuth = FirebaseAuth.getInstance();
mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Users");
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if(!TextUtils.isEmpty(sUser) && !TextUtils.isEmpty(sPass)){
signInVLogIn = true;
}else{
signInVLogIn = false;
}
if(user != null && signInVLogIn) {
}else{
//Toast.makeText(katiRoll.this, "Log In Failed", Toast.LENGTH_LONG).show();
}
}
};
sign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sUser = userEdit.getText().toString().trim();
sPass = passEdit.getText().toString().trim();
if(!TextUtils.isEmpty(sUser) && !TextUtils.isEmpty(sPass))
{
mAuth.signInWithEmailAndPassword(sUser, sPass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
checkUserValidation(dataSnapshot, sUser);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}else{
Toast.makeText(katiRoll.this, "User Login Doesn't Exist" , Toast.LENGTH_LONG).show();
}
}
});
}
}
});
reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(katiRoll.this, signUp.class));
}
});
}
private void checkUserValidation(DataSnapshot dataSnapshot, String emailForVer) {
Iterator iterator = dataSnapshot.getChildren().iterator();
while (iterator.hasNext())
{
DataSnapshot dataUser = (DataSnapshot)iterator.next();
if (dataUser.child("emailUser").getValue().toString().equals(emailForVer)){
if(dataUser.child("isVerified").getValue().toString().equals("unverified"))
{
Intent in = new Intent(katiRoll.this, Profile.class);
in.putExtra("USER KEY", dataUser.child("userKey").getValue().toString());
startActivity(in);
Log.d("USERKEY",dataUser.child("userKey").getValue().toString());
}
else{
startActivity(new Intent(katiRoll.this, welcome.class));
}
}
}
}
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
#Override
protected void onStop() {
super.onStop();
if(mAuthListener !=null){
mAuth.removeAuthStateListener(mAuthListener);
}
}
}
package com.example.akshay.katiroll.FirstScreen;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.akshay.katiroll.SecondScreen.welcome;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.example.akshay.katiroll.R;
import com.google.firebase.database.ValueEventListener;
import java.util.Iterator;
public class signUp extends AppCompatActivity {
EditText nUser, nPass;
Button registerBtn, backToMain;
ImageButton passVis;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
DatabaseReference mDatabaseRef, mUserCheckData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
nUser = (EditText) findViewById(R.id.nUser);
nPass = (EditText) findViewById(R.id.nPass);
registerBtn = (Button) findViewById(R.id.registerBtn);
backToMain = (Button) findViewById(R.id.backToMain);
passVis = (ImageButton)findViewById(R.id.passVis);
passVis.setImageResource(R.drawable.open);
passVis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable drawable = passVis.getDrawable();
if(drawable.getConstantState().equals(getResources().getDrawable(R.drawable.open,null).getConstantState())){
passVis.setImageResource(R.drawable.closed);
nPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}else{
passVis.setImageResource(R.drawable.open);
nPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD);
}
}
});
float Re = 255;
float Gr = 148;
float Bl = 0;
nUser.setTextColor(Color.rgb(Re, Gr, Bl));
nPass.setTextColor(Color.rgb(Re, Gr, Bl));
registerBtn.setTextColor(Color.rgb(Re, Gr, Bl));
backToMain.setTextColor(Color.rgb(Re, Gr, Bl));
mAuth = FirebaseAuth.getInstance();
mDatabaseRef = FirebaseDatabase.getInstance().getReference();
mUserCheckData = FirebaseDatabase.getInstance().getReference().child("User");
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
final String emailForVer = user.getEmail();
mUserCheckData.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
checkForValidation(dataSnapshot, emailForVer);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
Log.d("TAG", "onAuthStateChanged:signed_out");
}
}
};
registerBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String nUserString, nPassString;
nUserString = (nUser.getText().toString().trim());
nPassString = (nPass.getText().toString().trim());
if(nPassString.length()>=6){
if (!TextUtils.isEmpty(nUserString) && !TextUtils.isEmpty(nPassString)) {
mAuth.createUserWithEmailAndPassword(nUserString, nPassString).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
DatabaseReference mChildDatabase = mDatabaseRef.child("Users").push();
String key_user = mChildDatabase.getKey();
mChildDatabase.child("isVerified").setValue("unverified");
mChildDatabase.child("userKey").setValue(key_user);
mChildDatabase.child("EmailUser").setValue(nUserString);
mChildDatabase.child("PasswordUser").setValue(nPassString);
Toast.makeText(signUp.this, "User Account Created, Check Email For Confirmation", Toast.LENGTH_LONG).show();
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
user.sendEmailVerification();
}
startActivity(new Intent(signUp.this, Profile.class));
} else {
Toast.makeText(signUp.this, "Failed to create User Account", Toast.LENGTH_LONG).show();
}
}
});
} else {
Toast.makeText(signUp.this, "Failed to create User Account", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(signUp.this, "Failed to create User Account", Toast.LENGTH_LONG).show();
}
}
});
backToMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(signUp.this, katiRoll.class));
}
});
}
private void checkForValidation(DataSnapshot dataSnapshot, String emailForVer) {
Iterator iterator = dataSnapshot.getChildren().iterator();
while (iterator.hasNext())
{
DataSnapshot dataUser = (DataSnapshot)iterator.next();
if (dataUser.child("emailUser").getValue().toString().equals(emailForVer)){
if(dataUser.child("isVerified").getValue().toString().equals("unverified"))
{
Intent in = new Intent(signUp.this, Profile.class);
in.putExtra("USER KEY", dataUser.child("userKey").getValue().toString());
startActivity(in);
Log.d("USERKEY",dataUser.child("userKey").getValue().toString());
}
else{
startActivity(new Intent(signUp.this, welcome.class));
}
}
}
}
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
#Override
protected void onStop() {
super.onStop();
if(mAuthListener !=null){
mAuth.removeAuthStateListener(mAuthListener);
}
}
}
package com.example.akshay.katiroll.SecondScreen;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.akshay.katiroll.FirstScreen.katiRoll;
import com.example.akshay.katiroll.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class welcome extends AppCompatActivity {
Button logOutBtn;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
float Re = 255;
float Gr = 148;
float Bl = 0;
logOutBtn = (Button)findViewById(R.id.logOutBtn);
logOutBtn.setTextColor(Color.rgb(Re,Gr,Bl));
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user !=null) {
}else{
}
}
};
logOutBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mAuth.signOut();
finish();
startActivity(new Intent(welcome.this, katiRoll.class));
}
});
}
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
#Override
protected void onStop() {
super.onStop();
mAuth.removeAuthStateListener(mAuthListener);
}
}
The problem that is telling to you is that you are doing wrong the reference, you are trying to get something inside Firebase Database that is not there
This line is telling you that the child("Users").child(KeyUser); does not exist in your current DB structure
To fix this you should see your databse structure and then redo your reference.
if you post a photo of your Firebase DB i will be glad to refactor your code to your needs.
also here you are getting wrong your USER_KEY
keyUser = getIntent().getStringExtra("USER_KEY");
To fix this a short way would be doing this
keyUser = getIntent().getExtras().getString("USER_KEY");
you are geting an intent from where? if you are trying to get a putExtra from another Activity, you are doing it wrong, post where you are seting the extra ("USER_KEY");
Intent.getStringExtra() and Intent.getExtras() returns null if there are no extras, so you need to check for that before trying to call getString()
one quick example to check for extras first would be this
String keyUser;
Bundle extras = getIntent().getExtras();
if(extras == null) {
Log.e("keyUser is null","");
} else {
keyUser= extras.getString("USER_KEY");
}
happy coding

Categories