nullpointexception:error while inserting data into firebase database - java

i have provided a intent from the registration page to the add contacts page and in the add contacts page tried to add the details into the firebase database using two functions addusers() and addcontacts()
can somebody pls help me
this is my registration.java file
package universe.sk.syndriveapp;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class RegistrationActivity extends AppCompatActivity {
private EditText etName, etEmailsign, etPassign, etConfirmPassign, etBloodgroup, etDate;
private Button btn_register;
private TextView tvExist;
private FirebaseAuth firebaseAuth;
String name, email, password, bloodgrp, date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("SIGN UP");
setupUIViews();
firebaseAuth = FirebaseAuth.getInstance();
btn_register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (validate()) {
String user_email = etEmailsign.getText().toString().trim();
String user_password = etPassign.getText().toString().trim();
//store in database:to be done after filling the contacts
firebaseAuth.createUserWithEmailAndPassword(user_email, user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
finish();
startActivity(new Intent(RegistrationActivity.this, AddContacts.class));
//Toast.makeText(RegistrationActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(RegistrationActivity.this, "Registration Failed!", Toast.LENGTH_SHORT).show();
}
});
//startActivity(new Intent(RegistrationActivity.this,NavigationActivity.class));
}
}
});
tvExist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(RegistrationActivity.this, MainActivity.class));
}
});
}
private void setupUIViews() {
etName = findViewById(R.id.etName);
etEmailsign = findViewById(R.id.etEmailsign);
etPassign = findViewById(R.id.etPassign);
btn_register = findViewById(R.id.btn_register);
tvExist = findViewById(R.id.tvExist);
etConfirmPassign = findViewById(R.id.etConfirmPassign);
etDate = (EditText) findViewById(R.id.etDate);
etBloodgroup = (EditText) findViewById(R.id.etBloodgroup);
}
private Boolean validate() {
Boolean result = false;
bloodgrp = etBloodgroup.getText().toString().trim();
date = etDate.getText().toString().trim();
name = etName.getText().toString();
password = etPassign.getText().toString();
email = etEmailsign.getText().toString();
String confirmpass = etConfirmPassign.getText().toString();
if (name.isEmpty() || password.isEmpty() || email.isEmpty()) {
Toast.makeText(this, "Please enter all the details!", Toast.LENGTH_SHORT).show();
} else {
if (password.equals(confirmpass))
result = true;
else
Toast.makeText(this, "Confirm password doesn't match with your password!", Toast.LENGTH_SHORT).show();
}
return result;
}
}
this is my addcontacts java file
package universe.sk.syndriveapp;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
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.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class AddContacts extends AppCompatActivity {
Button registerbtn;
EditText etName,etEmailsign,etPassign,etBloodgroup,etDate;
EditText name1, num1, name2, num2, name3, num3;
String emname1,emname2,emname3;
String emnum1,emnum2,emnum3;
String name,email,password,bloodgrp,date;
private FirebaseAuth firebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addcontacts);
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.contacts);
actionBar.setTitle(" Add Emergency Contacts");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
registerbtn= findViewById(R.id.registerbtn);
name1 = findViewById(R.id.name1);
num1 = findViewById(R.id.num1);
name2 = findViewById(R.id.name2);
num2 = findViewById(R.id.num2);
name3 = findViewById(R.id.name3);
num3 = findViewById(R.id.num3);
setupUIViews();
firebaseAuth = FirebaseAuth.getInstance();
registerbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int value=checkData();
if(value==1) {
emname1 = name1.getText().toString();
emnum1 = num1.getText().toString();
emname2 = name2.getText().toString();
emnum2 = num2.getText().toString();
emname3 = name3.getText().toString();
emnum3 = num3.getText().toString();
bloodgrp = etBloodgroup.getText().toString().trim();
date = etDate.getText().toString().trim();
name = etName.getText().toString();
password = etPassign.getText().toString();
email = etEmailsign.getText().toString();
adduser();
addcontacts();
/*
if (emname1.isEmpty() || emname2.isEmpty() || emname3.isEmpty()){
startActivity(new Intent(AddContacts.this, RegistrationActivity.class));
Toast.makeText(AddContacts.this, "", Toast.LENGTH_SHORT).show();
}
*/
Toast.makeText(AddContacts.this, "Registration Success!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(AddContacts.this, NavigationActivity.class));
}
}
});
}
boolean isEmpty(EditText text){
CharSequence str = text.getText().toString();
return TextUtils.isEmpty(str);
}
int checkData(){
if(isEmpty(name1) || isEmpty(name2) || isEmpty(name3) || isEmpty(num1) || isEmpty(num2) || isEmpty(num3)){
Toast.makeText(AddContacts.this,"Please fill all contact details",Toast.LENGTH_SHORT).show();
return -1;
}
else
return 1;
}
private void setupUIViews()
{
etName = findViewById(R.id.etName);
etEmailsign = findViewById(R.id.etEmailsign);
etPassign = findViewById(R.id.etPassign);
etDate =(EditText)findViewById(R.id.etDate);
etBloodgroup =(EditText) findViewById(R.id.etBloodgroup);
}
private void addcontacts(){
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseusers = firebaseDatabase.getReference(firebaseAuth.getUid());
Contactdetails contactdetails;
contactdetails = new Contactdetails(emname1,emname2,emname3,emnum1,emnum2,emnum3);
databaseusers.setValue(contactdetails);
}
private void adduser(){
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseusers= firebaseDatabase.getReference(firebaseAuth.getUid());
Userinfo user;
user = new Userinfo(name,email,date,bloodgrp);
databaseusers.setValue(user);
}
}
this is my userinfo java file
package universe.sk.syndriveapp;
import android.net.Uri;
import com.google.android.gms.tasks.Task;
public class Userinfo {
public String username;
public String uemail;
public String udate;
public String bloodgroup;
// public Uri imageUri;
public Userinfo(){
}
public Userinfo(String username, String uemail, String udate, String bloodgroup) {
this.username = username;
this.uemail = uemail;
this.udate = udate;
this.bloodgroup = bloodgroup;
// this.imageUri = imageUri;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUemail() {
return uemail;
}
public void setUemail(String uemail) {
this.uemail = uemail;
}
public String getUdate(){
return udate;
}
public void setUdate(String udate) {
this.udate = udate;
}
public String getBloodgroup() {
return bloodgroup;
}
public void setBloodgroup(String bloodgroup) {
this.bloodgroup = bloodgroup;
}
/* public Uri getImageUri() {
return imageUri;
}
public void setImageUri(Uri imageUri) {
this.imageUri = imageUri;
} */
}
this is my contactdetails java file
package universe.sk.syndriveapp;
public class Contactdetails {
public String cname1;
public String cname2;
public String cname3;
public String no1;
public String no2;
public String no3;
public Contactdetails(){
}
public Contactdetails(String cname1, String cname2, String cname3, String no1, String no2, String no3) {
this.cname1 = cname1;
this.cname2 = cname2;
this.cname3 = cname3;
this.no1 = no1;
this.no2 = no2;
this.no3 = no3;
}
public void setCname1(String cname1) {
this.cname1 = cname1;
}
public void setCname2(String cname2) {
this.cname2 = cname2;
}
public void setCname3(String cname3) {
this.cname3 = cname3;
}
public void setNo1(String no1) {
this.no1 = no1;
}
public void setNo2(String no2) {
this.no2 = no2;
}
public void setNo3(String no3) {
this.no3 = no3;
}
public String getCname1() {
return cname1;
}
public String getCname2() {
return cname2;
}
public String getCname3() {
return cname3;
}
public String getNo1() {
return no1;
}
public String getNo2() {
return no2;
}
public String getNo3() {
return no3;
}
}
this is my addcontacts xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/hint_emname1"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_num1"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/name2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_emname2"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_num2"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/name3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_emname3"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/num3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_num3"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/registerbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginTop="25dp"
android:background="#color/colorPrimary"
android:text="#string/hint_registerbtn"
android:textColor="#FFFFFF" />
</LinearLayout>
this is my profile xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegistrationActivity">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="#string/user_name"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.106" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/etDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/date_of_birth"
android:inputType="date" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/etBloodgroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/blood_group"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/etEmailsign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etDate" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/etPassign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etEmailsign"
app:layout_constraintVertical_bias="0.08" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/etConfirmPassign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/confirm_password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etPassign"
app:layout_constraintVertical_bias="0.08" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorPrimary"
android:text="#string/next"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etConfirmPassign"
app:layout_constraintVertical_bias="0.132" />
<TextView
android:id="#+id/tvExist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="Already an existing member? Login"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_register" />
</LinearLayout>
finally this is my error log
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at universe.sk.syndriveapp.AddContacts$1.onClick(AddContacts.java:67)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22295)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Your addcontacts.xml doesn't have an EditText with ID etBloodgroup

The line
bloodgrp = etBloodgroup.getText().toString().trim();
causes the error.
You haven't done a findViewById() on the EditText bloodgroup before and it also does not exist in the addcontacts.xml file. If you have defined it in another xml file, you won't be able to call it in an Activity not associated with the xml source (and you cannot associate more than one xml layout file to an Activity).
So the answer is basically: Define the EditText in the xml file associated to the Activity where you are trying to handle it.

Related

My activity layout isn't showing when I run the app

I am currently building an Android app in Android Studio, written in Java, a symptom checker application where upon starting the application, user will be required to choose whether they want to open a patient account or a doctor account. For the patient account option, I managed to see the layout when I run the app but when I try to pick the doctor account option, the app does not show the layout. This is weird considering the fact that the code does not show any error, even the logcat too shows the process is perfectly fine.
Here is the activity class and the xml for the doctor account option, also with the main class. I am sorry if somehow the way I post this might be confusing, this is my first time asking a question here.
DoctorRegistrationActivity.java
`package com.example.jeusain;
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.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;
import com.google.firebase.database.DatabaseReference;
public class DoctorRegistrationActivity extends AppCompatActivity {
private TextView goToLogin;
private Button doctorRegisterButton;
private EditText doctor_name, university, placeofpractice, medicalregistrationnumber, doctor_email, doctor_password;
//firebase initialisation
private FirebaseAuth mAuth;
private FirebaseAuth mUser;
private DatabaseReference reference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_registration);
doctorRegisterButton = findViewById(R.id.doctor_registerbutton_page1);
doctor_name = findViewById(R.id.doctor_fullname);
university = findViewById(R.id.graduated_uni);
placeofpractice = findViewById(R.id.place_of_practice);
medicalregistrationnumber = findViewById(R.id.registration_number);
doctor_email = findViewById(R.id.doctor_email);
doctor_password = findViewById(R.id.doctor_password);
goToLogin = findViewById(R.id.loginFromDoctor);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),DoctorProfileActivity.class));
finish();
}
doctorRegisterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
performValidations();
Intent goToLogin = new Intent(DoctorRegistrationActivity.this, LoginActivity.class);
startActivity(goToLogin);
}
});
goToLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openLogin();
}
});
}
public void openLogin(){
Intent login = new Intent(DoctorRegistrationActivity.this, LoginActivity.class);
startActivity(login);
}
private void performValidations(){
String getDoctorname = doctor_name.getText().toString();
String getUniversity = university.getText().toString();
String getPlaceofPractice = placeofpractice.getText().toString();
String getMedicalregistrationnumber = medicalregistrationnumber.getText().toString();
String getDoctoremail = doctor_email.getText().toString();
String getDoctorpassword = doctor_password.getText().toString();
if (TextUtils.isEmpty(getDoctorname)){
doctor_name.setError("Name is required!");
return;
}
if (TextUtils.isEmpty(getUniversity)){
university.setError("University is required!");
return;
}
if (TextUtils.isEmpty(getPlaceofPractice)){
placeofpractice.setError("Place of Practice is required!");
return;
}
if (TextUtils.isEmpty(getMedicalregistrationnumber)){
medicalregistrationnumber.setError("Medical registration number is required!");
return;
}
if (TextUtils.isEmpty(getDoctoremail)){
doctor_email.setError("Email is required!");
return;
}
if (TextUtils.isEmpty(getDoctorpassword)){
doctor_password.setError("Password is required!");
return;
}
if (doctor_password.length()<8){
doctor_password.setError("Password must be 8 or more characters!");
return;
}
//register the user in firebase
mAuth.createUserWithEmailAndPassword(getDoctoremail, getDoctorpassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(DoctorRegistrationActivity.this, "User Created!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),DoctorProfileActivity.class));
}
else{
Toast.makeText(DoctorRegistrationActivity.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}`
activity_doctor_registration.xml
`<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a0db8e"
tools:context=".DoctorRegistrationActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/oval_background_shape"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title_DRP_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="You're a registered medical practitioner?"
android:textColor="#color/black"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/subtitle_DRP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="Please fill in the form with relevant information"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="#+id/title_DRP_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_DRP_1" />
<EditText
android:id="#+id/doctor_fullname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/subtitle_DRP1"
app:layout_constraintStart_toStartOf="#+id/subtitle_DRP1"
app:layout_constraintTop_toBottomOf="#+id/subtitle_DRP1" />
<EditText
android:id="#+id/graduated_uni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Graduated university"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/doctor_fullname"
app:layout_constraintStart_toStartOf="#+id/doctor_fullname"
app:layout_constraintTop_toBottomOf="#+id/doctor_fullname" />
<EditText
android:id="#+id/place_of_practice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Place of Practice"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/graduated_uni"
app:layout_constraintStart_toStartOf="#+id/graduated_uni"
app:layout_constraintTop_toBottomOf="#+id/graduated_uni" />
<EditText
android:id="#+id/registration_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Medical Registration Number"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/place_of_practice"
app:layout_constraintStart_toStartOf="#+id/place_of_practice"
app:layout_constraintTop_toBottomOf="#+id/place_of_practice" />
<EditText
android:id="#+id/doctor_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="E-mail address"
android:inputType="textEmailAddress"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/registration_number"
app:layout_constraintStart_toStartOf="#+id/registration_number"
app:layout_constraintTop_toBottomOf="#+id/registration_number" />
<EditText
android:id="#+id/doctor_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/doctor_email"
app:layout_constraintStart_toStartOf="#+id/doctor_email"
app:layout_constraintTop_toBottomOf="#+id/doctor_email" />
<Button
android:id="#+id/doctor_registerbutton_page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="REGISTER"
app:layout_constraintEnd_toEndOf="#+id/doctor_password"
app:layout_constraintStart_toStartOf="#+id/doctor_password"
app:layout_constraintTop_toBottomOf="#+id/doctor_password" />
<TextView
android:id="#+id/loginFromDoctor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login here"
tools:layout_editor_absoluteX="280dp"
tools:layout_editor_absoluteY="597dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
`
MainActivity.java
`package com.example.jeusain;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button choose_patient_account, choose_doctor_account;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
choose_patient_account = (Button) findViewById(R.id.patient_reg);
choose_doctor_account = (Button) findViewById(R.id.doctor_reg);
choose_patient_account.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openPatientRegister();
}
});
choose_doctor_account.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDoctorRegister();
}
});
}
public void openPatientRegister() {
Intent goToPatientRegister = new Intent(getApplicationContext(), PatientRegistrationActivity.class);
startActivity(goToPatientRegister);
}
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(getApplicationContext(), DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}
}`
Use:
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(this, DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}
Instead of
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(getApplicationContext(), DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}
Kristy Welsh's answer doesn't solve your problem then try specifying Activity name instead of "this"
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(MainActivity.this,DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}

Android Studio calculate total price

I wanna ask how can I sum all patty price and display in the total ? The total is in activity_main.xml layout while all the product price is in view_product.xml .
Screenshot of app
As you can see the grey color text is per patty price , and the right side black text is price after multiply the quantity of patty. I want all the black text to sum up and show at below total.
MainActivity.java
package com.tankarjian.it212n.a2910assingment;
import android.app.Activity;
import android.os.Bundle;
import android.widget.BaseAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends Activity {
private Products products;
private BaseAdapter productsAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
products = createInitialProductList();
productsAdapter = new ProductsAdapter(products, productClickListener, getLayoutInflater());
ListView productsListView = (ListView) findViewById(R.id.products_list);
productsListView.setAdapter(productsAdapter);
}
private Products createInitialProductList() {
return new Products(new ArrayList<>(Arrays.asList(
new Product("Chicken Patty", 0, 4.00),
new Product("Chicken Special Patty", 0, 5.00),
new Product("Imported Lamb Patty", 0, 8.00),
new Product("Imported Lamb Special Patty", 0, 10.00)
)));
}
private final ProductClickListener productClickListener = new ProductClickListener() {
#Override
public void onMinusClick(Product product) {
products.removeOneFrom(product);
productsAdapter.notifyDataSetChanged();
}
#Override
public void onPlusClick(Product product) {
products.addOneTo(product);
productsAdapter.notifyDataSetChanged();
}
};
private static class Products implements ProductDataSet {
private final List<Product> productList;
Products(List<Product> productList) {
this.productList = productList;
}
#Override
public int size() {
return productList.size();
}
#Override
public Product get(int position) {
return productList.get(position);
}
#Override
public long getId(int position) {
return position;
}
public void removeOneFrom(Product product) {
int i = productList.indexOf(product);
if (i == -1) {
throw new IndexOutOfBoundsException();
}
Product updatedProduct = new Product(product.name, (product.quantity - 1), product.getPrice());
productList.remove(product);
productList.add(i, updatedProduct);
}
public void addOneTo(Product product) {
int i = productList.indexOf(product);
if (i == -1) {
throw new IndexOutOfBoundsException();
}
Product updatedProduct = new Product(product.name, (product.quantity + 1), product.getPrice());
productList.remove(product);
productList.add(i, updatedProduct);
}
}
}
ProductsAdapter.java
package com.tankarjian.it212n.a2910assingment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Locale;
class ProductsAdapter extends BaseAdapter {
private final ProductDataSet productDataSet;
private final ProductClickListener productClickListener;
private final LayoutInflater layoutInflater;
ProductsAdapter(ProductDataSet productDataSet, ProductClickListener productClickListener, LayoutInflater layoutInflater) {
this.productDataSet = productDataSet;
this.productClickListener = productClickListener;
this.layoutInflater = layoutInflater;
}
#Override
public int getCount() {
return productDataSet.size();
}
#Override
public Product getItem(int position) {
return productDataSet.get(position);
}
#Override
public long getItemId(int position) {
return productDataSet.getId(position);
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = createView(parent);
view.setTag(ViewHolder.from(view));
}
Product Products =(Product) getItem(position);
ImageView image = (ImageView) view.findViewById(R.id.imageBurgerView);
image.setImageResource(R.drawable.burger);
TextView text = (TextView) view.findViewById(R.id.singlePrice);
String price = String.format(Locale.ENGLISH, "%.2f", Products.getPrice());
text.setText(price);
TextView text1 = (TextView) view.findViewById(R.id.totalPrice);
String totalPrice = String.format(Locale.ENGLISH, "%.2f", (Products.getPrice() * Products.quantity));
text1.setText(totalPrice);
Product product = productDataSet.get(position);
ViewHolder viewHolder = (ViewHolder) view.getTag();
update(viewHolder, product);
return view;
}
private View createView(ViewGroup parent) {
return layoutInflater.inflate(R.layout.view_product, parent, false);
}
private void update(ViewHolder viewHolder, final Product product) {
viewHolder.name.setText(product.name);
viewHolder.quantity.setText(String.valueOf(product.quantity));
viewHolder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
productClickListener.onMinusClick(product);
}
});
viewHolder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
productClickListener.onPlusClick(product);
}
});
}
private static final class ViewHolder {
final TextView name;
final TextView quantity;
final View minus;
final View plus;
static ViewHolder from(View view) {
return new ViewHolder(
((TextView) view.findViewById(R.id.product_name)),
((TextView) view.findViewById(R.id.product_quantity)),
view.findViewById(R.id.product_minus),
view.findViewById(R.id.product_plus)
);
}
private ViewHolder(TextView name, TextView quantity, View minus, View plus) {
this.name = name;
this.quantity = quantity;
this.minus = minus;
this.plus = plus;
}
}
}
Product.java
package com.tankarjian.it212n.a2910assingment;
class Product {
final String name;
final int quantity;
private double price;
Product(String name, int quantity, Double price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public double getPrice() {return price;}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="16dp"
tools:context=".MainActivity">
<ListView
android:id="#+id/products_list"
android:layout_width="0dp"
android:layout_height="512dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Switch
android:id="#+id/switchMember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="Member"
app:layout_constraintEnd_toEndOf="#+id/products_list"
app:layout_constraintTop_toBottomOf="#+id/products_list" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginStart="16dp"
android:text="Tax "
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/products_list"
app:layout_constraintVertical_bias="0.92" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginBottom="68dp"
android:text="Total "
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/totalTax"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="RM"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/totalTax"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView7"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="RM"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/finalTotalPrice"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toTopOf="#+id/textView8" />
<TextView
android:id="#+id/finalTotalPrice"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="#+id/textView8" />
<TextView
android:id="#+id/textView7"
android:layout_width="210dp"
android:layout_height="25dp"
android:layout_marginStart="20dp"
android:text="--------------------------------"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/totalTax"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="#+id/textView2" />
<TextView
android:id="#+id/textView8"
android:layout_width="210dp"
android:layout_height="26dp"
android:layout_marginStart="20dp"
android:text="--------------------------------"
android:textColor="#2A2F49"
android:textSize="19sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toStartOf="#+id/finalTotalPrice"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="#+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>
view_product.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF5722"
android:textSize="20sp"
android:textStyle="bold|italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageBurgerView"
android:layout_width="70dp"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:contentDescription="#string/todo"
app:srcCompat="#drawable/burger" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="#string/rm"
android:textAlignment="textEnd"
android:textColor="#878BA3"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/singlePrice"
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_weight="1"
android:text="#string/textview"
android:textColor="#878BA3"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/rm"
android:textAlignment="textEnd"
android:textColor="#272E53"
android:textSize="16sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/totalPrice"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/textview"
android:textAlignment="textStart"
android:textColor="#272E53"
android:textSize="16sp"
android:textStyle="bold|italic" />
</LinearLayout>
<LinearLayout
android:layout_width="116dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/product_minus"
android:layout_width="30dp"
android:layout_height="40dp"
android:text="-"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="#+id/product_quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="20"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="#+id/product_plus"
android:layout_width="30dp"
android:layout_height="40dp"
android:text="+"
tools:ignore="TouchTargetSizeCheck" />
</LinearLayout>
</LinearLayout>
In view_product.xml you are only displaying the price, but that's not where the actual data is located so your question shouldn't be that the prices are in view_product.xml and you want to show in activity_main.xml, but your question should be -
Where is the data located
How to make the necessary calculations
Where to show it. .
Your data regarding the prices are in products object. So, in order to calculate the total you just need to add one more method which calculates the total
private double calculateTotal(ProductDataSet productDataSet) {
double totalPrice = 0.0;
for(int i=0; i<productDataSet.size(); i++) {
totalPrice += productDataSet.get(i).getPrice();
}
return totalPrice;
}
The result of this you can store in a new value or show directly in the textView itself.
((TextView)findViewById(R.id.finalTotalPrice)).setText(calculateTotal(products).toString());
This you can call anywhere after createInitialProductList(). I hope this helps and makes it clear for you.

Firestore data can't display on RecyclerView

The app quit when I change the line I am facing a problem when I try to develop an Android App that display the data from Firestore.
.
Can I know why it is not displaying the data on my RecyclerView and EventPage? I have rechecked everything, but I still can't find out why data doesn't show on EventPage.
EventPage.java
public class EventPage extends AppCompatActivity {
private static final String TAG = "DashboardFragment";
private FirestoreRecyclerAdapter<EventModel, EventViewHolder> adapter;
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_page);
//Set recyclerView
recyclerView = findViewById(R.id.recyclerEventCard);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference cRef = db.collection("event");
Query query = db.collection("event").orderBy("ASC").limit(10);
FirestoreRecyclerOptions<EventModel> options = new FirestoreRecyclerOptions.Builder<EventModel>()
.setQuery(query, EventModel.class)
.build();
adapter = new FirestoreRecyclerAdapter<EventModel, EventViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull EventViewHolder holder, int position, #NonNull EventModel model) {
holder.textViewEName.setText(model.getEName());
holder.textViewEDate.setText(model.getEDate());
holder.textViewEVenue.setText(model.getEVenue());
holder.textViewEDesc.setText(model.getEDesc());
}
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//Changes on XML file. Try it.
View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.event_card_list, parent, false);
return new EventViewHolder(v);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
}
EventModel.java
package com.example.myvolunteer;
public class EventModel {
private String EName;
private String EDate;
private String EVenue;
private String EDesc;
//Constructor
public EventModel(){}
public EventModel(String EName, String EDate, String EVenue, String EDesc){
this.EName = EName;
this.EDate = EDate;
this.EVenue = EVenue;
this.EDesc = EDesc;
}
public String getEName() {
return EName;
}
public void setEName(String EName) {
this.EName = EName;
}
public String getEDate() {
return EDate;
}
public void setEDate(String EDate) {
this.EDate = EDate;
}
public String getEVenue() {
return EVenue;
}
public void setEVenue(String EVenue) {
this.EVenue = EVenue;
}
public String getEDesc() {
return EDesc;
}
public void setEDesc(String EDesc) {
this.EDesc = EDesc;
}
}
EventViewHolder.java
package com.example.myvolunteer;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.NotNull;
class EventViewHolder extends RecyclerView.ViewHolder{
TextView textViewEName, textViewEVenue, textViewEDate, textViewEDesc;
public EventViewHolder(#NonNull #NotNull View itemView) {
super(itemView);
textViewEName = itemView.findViewById(R.id.textEventName);
textViewEDate = itemView.findViewById(R.id.textEventDate);
textViewEVenue = itemView.findViewById(R.id.textEventVenue);
textViewEDesc = itemView.findViewById(R.id.textEventDesc);
}
}
activity_event_page.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EventPage">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerEventCard"
android:layout_width="351dp"
android:layout_height="663dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
event_card_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content"
>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="250dp"
app:cardBackgroundColor="#B7F1FE"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="432dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textEventName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Event Name"
android:textColor="#color/black"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textEventDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:text="Date"
android:textColor="#color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/textEventVenue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:text="Venue"
android:textColor="#color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/textEventDesc"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:text="Description"
android:textColor="#color/black"
android:textSize="20sp" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp">
<TextView
android:id="#+id/createText"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Created By: "
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textEventCreatedBy"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="Leader Name"
android:textColor="#color/black"
android:textSize="15sp"
app:layout_constraintStart_toEndOf="#+id/createText"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:backgroundTint="#adcae6"
android:text="More Info"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#color/black">
</Button>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
make Sure your model class String variables are equal to your firestore data feild because it is very important to retrieve data from firestore
private String eventName;
private String eventDate;
private String eventVenue;
private String eventDescription;
//Constructor
public EventModel(){}
public EventModel(String eventName, String eventDate, String eventVenue, String eventDescription){
this.eventName = eventName;
this.eventDate = eventDate;
this.eventVenue = eventVenue;
this.eventDescription = eventDescription;
}
public String geteventName() {
return eventName;
}
public void seteventName(String eventName) {
this.eventName = eventName;
}
public String geteventDate() {
return eventDate;
}
public void seteventDate(String eventDate) {
this.eventDate = eventDate;
}
public String geteventVenue() {
return eventVenue;
}
public void seteventVenue(String eventVenue) {
this.eventVenue = eventVenue;
}
public String geteventDescription() {
return eventDescription;
}
public void seteventDescription(String eventDescription) {
this.eventDescription = eventDescription;
}
This looks wrong:
Query query = db.collection("event").orderBy("ASC").limit(10);
You are telling Firestore to sort by a field called ASC, which doesn't exist in your screenshot, so no documents are returned.
You probably want to sort on the ascending date, which you can do with:
Query query = db.collection("event").orderBy("eventDate", Direction.ASCENDING).limit(10);
Note that while the above will fix the problem of the documents not showing, they will still show up in the wrong order. This is because your eventDate field is not useful for sorting dates.
Strings in Firestore are sorted lexicographically, and in that order, "12/7/2021" becomes before "13/6/2021".
If you want to fix this problem, you can either store the dates as Firestore's built-in Timestamp type, or use a string format that allows sorting, like "2021-07-12" (which is *after* "2021-07-13"`.

Data binding user model and login

I am trying to incorporate a user model into my application while using data binding. And I am trying to get the user to sign up using firebase authentication email and password. But when my sign up button is clicked nothing happens.
I have a way for my login activity working using my own custom observable string class but I am just trying out a different method for this.
my user model:
public class User extends BaseObservable {
private String email;
private String password;
private String confPassword;
private String username;
#Bindable
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
notifyPropertyChanged(BR.email);
}
#Bindable
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
notifyPropertyChanged(BR.password);
}
#Bindable
public String getConfPassword() {
return confPassword;
}
public void setConfPassword(String confPassword) {
this.confPassword = confPassword;
notifyPropertyChanged(BR.confPassword);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
signUpViewModel:
public class SignUpViewModel extends ViewModel {
public void firebaseSignUp(String email, String password, String confPassword){
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(confPassword)) {
if (android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
if (password.length() > 6) {
if (password.equals(confPassword)) {
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
} else {
}
}
});
}
}
}
}
}
}
signUpActivity:
public class SignUpActivity extends AppCompatActivity {
private SignUpViewModel signUpViewModel;
private FirebaseAuth.AuthStateListener authStateListener;
private User user = new User();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivitySignUpBinding signUpBinding = DataBindingUtil.setContentView(this, R.layout.activity_sign_up);
signUpViewModel = new SignUpViewModel();
signUpBinding.setVModel(signUpViewModel);
signUpBinding.setActivity(this);
// signUpBinding.setUser(user);
signUpBinding.executePendingBindings();
}
public void signUp(){
signUpViewModel.firebaseSignUp(user.getEmail(), user.getPassword(), user.getConfPassword());
authStateListener = auth -> {
FirebaseUser firebaseUser = auth.getCurrentUser();
if (firebaseUser != null){
Intent sendToMain = new Intent(SignUpActivity.this, MainActivity.class);
startActivity(sendToMain);
finish();
}
};
FirebaseAuth.getInstance().addAuthStateListener(authStateListener);
}
#Override
protected void onStop() {
super.onStop();
FirebaseAuth.getInstance().removeAuthStateListener(authStateListener);
}
public void toSignIn(){
Intent toSignIn = new Intent(this, LoginActivity.class);
startActivity(toSignIn);
finish();
}
}
xml file with databindings:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="vModel"
type="com.jthomann.cff_mvvm1.viewModel.SignUpViewModel"/>
<variable name="activity"
type="com.jthomann.cff_mvvm1.view.SignUpActivity"/>
<variable name="userModel"
type="com.jthomann.cff_mvvm1.model.User"/>
<import type="android.view.View"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/cff_bg_new"
tools:context="com.jthomann.cff_mvvm1.view.SignUpActivity"
tools:layout_editor_absoluteY="25dp">
<Button
android:id="#+id/signUpBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#drawable/sign_up_btn_new"
android:textAlignment="center"
android:textColor="#android:color/white"
android:onClick="#{() -> activity.signUp()}"
app:layout_constraintEnd_toEndOf="#+id/signUpConfPass"
app:layout_constraintStart_toStartOf="#+id/signUpConfPass"
app:layout_constraintTop_toBottomOf="#+id/signUpConfPass" />
<EditText
android:id="#+id/signUpEmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="33dp"
android:ems="10"
android:inputType="textEmailAddress"
android:text="#={userModel.email}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/tvEmail"
app:layout_constraintTop_toBottomOf="#+id/tvEmail"
tools:ignore="Autofill,LabelFor" />
<TextView
android:id="#+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginTop="16dp"
android:background="#null"
android:fontFamily="#font/montserrat"
android:text="#string/email"
android:textColor="#01BAEF"
android:textSize="13sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo" />
<TextView
android:id="#+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#null"
android:fontFamily="#font/montserrat"
android:text="#string/password"
android:textColor="#01BAEF"
android:textSize="13sp"
app:layout_constraintStart_toStartOf="#+id/signUpEmail"
app:layout_constraintTop_toBottomOf="#+id/signUpEmail" />
<TextView
android:id="#+id/tvConfPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#null"
android:fontFamily="#font/montserrat"
android:text="#string/confPass"
android:textColor="#01BAEF"
android:textSize="13sp"
app:layout_constraintStart_toStartOf="#+id/signUpPassword"
app:layout_constraintTop_toBottomOf="#+id/signUpPassword" />
<TextView
android:id="#+id/tvToSignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#null"
android:fontFamily="#font/montserrat"
android:text="#string/sign_in"
android:textColor="#color/colorOffWhite"
android:textSize="14sp"
android:onClick="#{() -> activity.toSignIn()}"
app:layout_constraintEnd_toEndOf="#+id/signUpBtn"
app:layout_constraintStart_toStartOf="#+id/signUpBtn"
app:layout_constraintTop_toBottomOf="#+id/signUpBtn" />
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:contentDescription="#null"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/cff_logo_new" />
<EditText
android:id="#+id/signUpPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:labelFor="#id/signUpPassword"
android:text="#={userModel.password}"
app:layout_constraintEnd_toEndOf="#+id/signUpEmail"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/tvPassword"
app:layout_constraintTop_toBottomOf="#+id/tvPassword"
tools:ignore="Autofill" />
<EditText
android:id="#+id/signUpConfPass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:labelFor="#id/signUpConfPass"
android:inputType="textPassword"
android:text="#={userModel.confPassword}"
app:layout_constraintEnd_toEndOf="#+id/signUpPassword"
app:layout_constraintStart_toStartOf="#+id/tvConfPassword"
app:layout_constraintTop_toBottomOf="#+id/tvConfPassword"
tools:ignore="Autofill" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
well found the answer, I was just missing a line of code:
signUpBinding.setUser(user);
works after I added this in.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()'

After reaching the registration page and entering details the app crashes with a null pointer exception. I did search for this question on the site, but I wasn't able to find a suitable solution. It would be amazing if someone could help me sort out this issue.
This is my registration activity Java file:
package universe.sk.syndriveapp;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
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.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;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class RegistrationActivity extends AppCompatActivity {
private EditText etName,etEmailsign,etPassign,etBloodgroup,etDate;
private Button btn_register;
private TextView tvExist;
private FirebaseAuth firebaseAuth;
String name,email,password,bloodgrp,date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("SIGN UP");
setupUIViews();
firebaseAuth = FirebaseAuth.getInstance();
btn_register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(validate())
{
String user_email = etEmailsign.getText().toString().trim();
String user_password = etPassign.getText().toString().trim();
//store in database:to be done after filling the contacts
firebaseAuth.createUserWithEmailAndPassword(user_email,user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
adduser();
Toast.makeText(RegistrationActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();}
else
Toast.makeText(RegistrationActivity.this, "Registration Failed!", Toast.LENGTH_SHORT).show();
}
});
startActivity(new Intent(RegistrationActivity.this,NavigationActivity.class));
}
}
});
tvExist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(RegistrationActivity.this,MainActivity.class));
}
});
}
private void setupUIViews()
{
etName = (EditText)findViewById(R.id.etName);
etEmailsign =(EditText) findViewById(R.id.etEmailsign);
etPassign =(EditText) findViewById(R.id.etPassign);
btn_register = (Button)findViewById(R.id.btn_register);
tvExist = (TextView)findViewById(R.id.tvExist);
etDate =(EditText)findViewById(R.id.etDate);
etBloodgroup =(EditText)findViewById(R.id.etBloodgroup);
}
private Boolean validate()
{
Boolean result = false;
name = etName.getText().toString();
password = etPassign.getText().toString();
email = etEmailsign.getText().toString();
bloodgrp = etBloodgroup.getText().toString().trim();
date = etDate.getText().toString().trim();
if(name.isEmpty() || password.isEmpty() || email.isEmpty())
{
Toast.makeText(this, "Please enter all the details!", Toast.LENGTH_SHORT).show();
}
else
result=true;
return result;
}
private void adduser(){
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseusers= firebaseDatabase.getReference(firebaseAuth.getUid());
Userinfo user = new Userinfo(name,bloodgrp,date,email,password);
databaseusers.setValue(user);
}
}
This is my Userinfo Java file:
package universe.sk.syndriveapp;
public class Userinfo {
public String username;
public String uemail;
public String udate;
public String upassword;
public String bloodgroup;
public Userinfo(String username, String uemail, String udate, String upassword, String bloodgroup) {
this.username = username;
this.uemail = uemail;
this.udate = udate;
this.upassword = upassword;
this.bloodgroup = bloodgroup;
}
public String getUsername() {
return username;
}
public String getUemail() {
return uemail;
}
public String getUdate() {
return udate;
}
public String getUpassword() {
return upassword;
}
public String getBloodgroup() {
return bloodgroup;
}
}
This is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegistrationActivity">
<EditText
android:id="#+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="#string/user_name"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.106" />
<EditText
android:id="#+id/etDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/date_of_birth"
android:inputType="date" />
<EditText
android:id="#+id/etBloodGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/blood_group"
android:inputType="text" />
<EditText
android:id="#+id/etEmailsign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etDate" />
<EditText
android:id="#+id/etPassign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etEmailsign"
app:layout_constraintVertical_bias="0.08" />
<EditText
android:id="#+id/etConfirmPassign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/confirm_password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etPassign"
app:layout_constraintVertical_bias="0.08" />
<Button
android:id="#+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorPrimary"
android:text="#string/next"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etConfirmPassign"
app:layout_constraintVertical_bias="0.132" />
<TextView
android:id="#+id/tvExist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="Already an existing member? Login"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_register" />
</LinearLayout>
error :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at universe.sk.syndriveapp.RegistrationActivity.validate(RegistrationActivity.java:96)
at universe.sk.syndriveapp.RegistrationActivity.access$000(RegistrationActivity.java:22)
at universe.sk.syndriveapp.RegistrationActivity$1.onClick(RegistrationActivity.java:44)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22295)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
You didn't initialize etBloodgroup. That's why you are getting NullPointerException.
Initialize your views properly:
etBloodgroup =(EditText) findViewById(R.id.etBloodGroup);
There is another EditText with id etConfirmPassign. You have to initialize all the views that you want to access.

Categories

Resources