我提供了从注册页面到添加联系人页面的意图,并在添加联系人页面中尝试使用两个功能addusers()和addcontacts()将详细信息添加到Firebase数据库中 有人可以帮我吗
这是我的registration.java文件
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;
}
}
这是我的addcontacts java文件
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);
}
}
这是我的userinfo java文件
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;
} */
}
这是我的contactdetails Java文件
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;
}
}
这是我的addcontacts 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"
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>
这是我的个人资料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: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>
最后这是我的错误日志
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)
答案 0 :(得分:1)
您的addcontacts.xml
没有ID为 etBloodgroup
EditText
答案 1 :(得分:0)
行
bloodgrp = etBloodgroup.getText().toString().trim();
导致错误。
您以前没有在findViewById()
bloodgroup 上完成过EditText
,而且在addcontacts.xml
文件中也不存在。如果您在另一个xml文件中定义了它,则将无法在未与xml源关联的Activity
中调用它(并且不能将多个xml布局文件与Activity
关联) )。
所以答案基本上是:在与您尝试处理的EditText
相关的xml文件中定义Activity
。