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

Time:01-13

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);
    }
}`

CodePudding user response:

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);
}

CodePudding user response:

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);
}
  • Related