我目前正在Android Studio中构建一个用Java编写的Android应用程序,这是一个症状检查器应用程序,在启动应用程序时,用户需要选择是要打开患者帐户还是医生帐户。对于患者帐户选项,我运行应用程序时设法看到了布局,但当我尝试选择医生帐户选项时,应用程序没有显示布局。考虑到代码没有显示任何错误,甚至logcat也显示过程完全正常,这很奇怪。
这里是活动类和医生账户选项的xml,也是主类。如果我发布这篇文章的方式可能会让人困惑,我很抱歉,这是我第一次在这里问问题。
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);
}
}`
2条答案
按热度按时间ngynwnxp1#
用途:
代替
wko9yo5t2#
Kristy Welsh的答案不能解决您的问题,请尝试指定活动名称而不是“this”