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

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

Related

Intent/Start activity starting the wrong activity?

For some reason, my start activity sends me to the wrong activity( sending me to add task class vs review task class) no error and following in log cat doesn't help me track down the issue. I tried using startActivityForResult(); but that did not help at all and had the same issue. It goes to createTask.class. I need to put more details but I am not sure what else to put, Maybe the put extra and putting two items packed to the instance, but I don't think that would be an issue
<?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=".MainActivity">
<TextView
android:id="#+id/amountOfTasksTextViewUIMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You Have 0 Tasks"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04000002" />
<Button
android:id="#+id/viewTaskButtonUIMain"
android:layout_width="209dp"
android:layout_height="46dp"
android:text="#string/viewTaskButtonUIMainString"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26999998" />
<Button
android:id="#+id/createTaskButtonUIMain"
android:layout_width="209dp"
android:layout_height="46dp"
android:text="#string/createTaskButtonUIMainString"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/viewTaskButtonUIMain"
app:layout_constraintStart_toStartOf="#+id/viewTaskButtonUIMain"
app:layout_constraintTop_toBottomOf="#+id/viewTaskButtonUIMain"
app:layout_constraintVertical_bias="0.04000002" />
<TextView
android:id="#+id/upCommingTasksTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/upCommingTasksTextViewUIMainActivityString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/amountOfTasksTextViewUIMain"
app:layout_constraintVertical_bias="0.18" />
<TextView
android:id="#+id/taskNameTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintStart_toStartOf="#+id/upCommingTasksTextViewUIMainActivity"
app:layout_constraintTop_toBottomOf="#+id/upCommingTasksTextViewUIMainActivity"
app:layout_constraintVertical_bias="0.100000024" />
<TextView
android:id="#+id/dateTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintEnd_toEndOf="#+id/taskNameTextViewUIMainActivity"
app:layout_constraintStart_toStartOf="#+id/taskNameTextViewUIMainActivity"
app:layout_constraintTop_toTopOf="#+id/taskNameTextViewUIMainActivity"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/priorityTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toEndOf="#+id/dateTextViewUIMainActivity"
app:layout_constraintTop_toBottomOf="#+id/taskNameTextViewUIMainActivity" />
</androidx.constraintlayout.widget.ConstraintLayout>
/*
HW02
MainActivity.java
*/
package com.example.hw02;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
public class MainActivity extends AppCompatActivity {
//UI Vars
Button viewTaskButtonUIMain;
Button createTaskButtonUIMain;
TextView amountOfTasksTextViewUIMain;
TextView taskNameTextViewUIMainActivity;
TextView dateTextViewUIMainActivity;
TextView priorityTextViewUIMainActivity;
//Java Vars
ArrayList<Tasks> taskArrayList;
ArrayList<String> taskNameList;
int selectedTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//UI Vars inst
viewTaskButtonUIMain = findViewById(R.id.viewTaskButtonUIMain);
createTaskButtonUIMain = findViewById(R.id.createTaskButtonUIMain);
amountOfTasksTextViewUIMain = findViewById(R.id.amountOfTasksTextViewUIMain);
dateTextViewUIMainActivity = findViewById(R.id.dateTextViewUIMainActivity);
priorityTextViewUIMainActivity = findViewById(R.id.priorityTextViewUIMainActivity);
taskNameTextViewUIMainActivity = findViewById(R.id.taskNameTextViewUIMainActivity);
//Java Vars inst
taskArrayList = new ArrayList<Tasks>();
//create Tasks
createTaskButtonUIMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sendArray1 = new Intent(MainActivity.this,createTask.class );
sendArray1.putExtra("reviewActivityArray",taskArrayList);
startActivity(sendArray1);
}
});
viewTaskButtonUIMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] taskNames = new String[taskArrayList.size()];
for(int i = 0; i < taskNames.length; i++) {
taskNames[i] = taskArrayList.get(i).getTaskName();
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select Task")
.setItems(taskNames, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent taskDelete = new Intent(MainActivity.this, reviewActivity.class );
taskDelete.putExtra("catch1", taskArrayList);
taskDelete.putExtra("catch2",which);
startActivity(taskDelete);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
});
amountOfTasksTextViewUIMain.setText("You have "+ taskArrayList.size() + " tasks");
//This is for the Create Task
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("catch")){
taskArrayList = (ArrayList<Tasks>)getIntent().getSerializableExtra("catch");
int vartempsize = taskArrayList.size();//delete
amountOfTasksTextViewUIMain.setText("You have "+ taskArrayList.size() + " tasks");
Collections.sort(taskArrayList);
//if the arrayList isn't empty
if(taskArrayList.isEmpty()==false){
//change the text
String tempName = taskArrayList.get(0).getTaskName();
taskNameTextViewUIMainActivity.setText(tempName);
dateTextViewUIMainActivity.setText(taskArrayList.get(0).returnDate().toString());
priorityTextViewUIMainActivity.setText(taskArrayList.get(0).getPrioty());
}
}
//comming from review
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("dead")){
taskArrayList = (ArrayList<Tasks>) getIntent().getSerializableExtra("dead");
Collections.sort(taskArrayList);
}
}
private void sortLists(){
//sorts list
Collections.sort(taskArrayList);
//organize the names
taskNameList.clear();
//adds all the names in the same order as the taskArrayList
for (Tasks var : taskArrayList){
taskNameList.add(var.getTaskName());
}
}
}
<?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=".reviewActivity">
<TextView
android:id="#+id/TaskInformationTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/taskInfromationTextViewUIactivityReviewString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.13"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06999999" />
<TextView
android:id="#+id/nameTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nameTextViewUICreateTextString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/TaskInformationTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/TaskInformationTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.100000024" />
<TextView
android:id="#+id/dateTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dateTextViewUIactivityReviewString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/nameTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/nameTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.050000012" />
<TextView
android:id="#+id/priorityTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/priorityTextViewUIactivityReviewString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/dateTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/dateTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.07999998" />
<TextView
android:id="#+id/nameTaskTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/nameTextViewUIactivityReview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/nameTextViewUIactivityReview" />
<TextView
android:id="#+id/dateTaskTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/dateTextViewUIactivityReview"
app:layout_constraintStart_toStartOf="#+id/nameTaskTextViewUIactivityReview" />
<TextView
android:id="#+id/priorityTaskTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/priorityTextViewUIactivityReview"
app:layout_constraintStart_toStartOf="#+id/dateTaskTextViewUIactivityReview" />
<Button
android:id="#+id/cancelButtonUIactivityReview"
android:layout_width="114dp"
android:layout_height="54dp"
android:text="#string/cancleButtonUICreateTaskString"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.09"
app:layout_constraintStart_toStartOf="#+id/priorityTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/priorityTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.16000003" />
<Button
android:id="#+id/deleteButtonUIactivityReview"
android:layout_width="106dp"
android:layout_height="49dp"
android:text="#string/deleteButtonUIactivityReviewString"
app:layout_constraintBottom_toBottomOf="#+id/cancelButtonUIactivityReview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.58"
app:layout_constraintStart_toEndOf="#+id/cancelButtonUIactivityReview" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.hw02;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class reviewActivity extends AppCompatActivity {
ArrayList<Tasks> taskArrayList;
int plank;
Button deleteButtonUIactivityReview;
Button cancelButtonUIactivityReview;
TextView nameTaskTextViewUIactivityReview;
TextView dateTaskTextViewUIactivityReview;
TextView priorityTaskTextViewUIactivityReview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_review);
deleteButtonUIactivityReview = findViewById(R.id.deleteButtonUIactivityReview);
cancelButtonUIactivityReview = findViewById(R.id.cancelButtonUIactivityReview);
deleteButtonUIactivityReview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
taskArrayList.remove(plank);
Intent goHomeDelete = new Intent(reviewActivity.this,MainActivity.class);
goHomeDelete.putExtra("dead",taskArrayList);
startActivity(goHomeDelete);
}
});
cancelButtonUIactivityReview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent goHomeSafe = new Intent(reviewActivity.this,MainActivity.class);
startActivity(goHomeSafe);
}
});
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("catch1")){
taskArrayList = (ArrayList<Tasks>) getIntent().getSerializableExtra("catch1");
plank = (int)getIntent().getSerializableExtra("catch2");
}
nameTaskTextViewUIactivityReview.setText(taskArrayList.get(plank).taskName);
dateTaskTextViewUIactivityReview.setText(taskArrayList.get(plank).returnDate().toString());
priorityTaskTextViewUIactivityReview.setText(taskArrayList.get(plank).getPrioty());
}
}

nullpointexception:error while inserting data into firebase database

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.

Why am I getting thread exiting with uncaught exception?

I am trying to open another activity according to the sap number entered by the user. But even before the next activity gets opened I get an exception, illegal state exception and thread exiting with uncaught exception.
This is the java file from where I am calling the other activities-
package ai.app;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
public class homepage extends Activity {
SQLiteDatabase db;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
String sap = getIntent().getExtras().getString("sapno");
tv=(TextView)findViewById(R.id.textView1);
db=openOrCreateDatabase("aidatabase.db",MODE_PRIVATE,null);
Cursor c=db.rawQuery("select * from master where sapno='"+sap+"'", null);
Boolean s;
s=c.moveToFirst();
String a=c.getString(c.getColumnIndex("department"));
tv.setText("Welcome to "+a+" Department");
}
public void call (View v)
{
switch(v.getId())
{
case R.id.view:
{
Toast.makeText(this, "view done", Toast.LENGTH_LONG).show();
Intent i=new Intent(this, viewdetails.class);
startActivity(i);
finish();
break;
}
case R.id.enter:
{
Intent i = getIntent();
String sap = i.getStringExtra("sapno");
if(sap.equals("102"))
{
System.out.println("Inside cargo");
Intent i1=new Intent(this, cargo.class);
startActivity(i1);
finish();
}
else if(sap.equals("103"))
{
System.out.println("Inside hcc");
Intent i1=new Intent(this, hcc.class);
startActivity(i1);
finish();
}
else if(sap.equals("104"))
{
System.out.println("Inside trimmers");
Intent i1=new Intent(this, trimmers.class);
startActivity(i1);
finish();
}
else if(sap.equals("105"))
{
System.out.println("Inside ops 1");
Intent i1=new Intent(this, ops.class);
System.out.println("Inside ops 2");
startActivity(i1);
finish();
}
else if(sap.equals("107"))
{
System.out.println("Inside routing");
Intent i1=new Intent(this, routing.class);
startActivity(i1);
finish();
}
}
}
}
}
For example if ops section gets selected, the exception is thrown.
Following is the java code of ops.java
package ai.app;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
public class ops extends Activity
{ #Override
protected void onCreate(Bundle savedInstanceState)
{ System.out.println("inside oncreate");
try { System.out.println("inside try1");
super.onCreate(savedInstanceState);
System.out.println("inside try2");
setContentView(R.layout.opssection); }
catch(Exception e)
{ Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show(); }
}
}
The xml file for the same is -
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="3dp"
android:text="Remarks" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="200dp"
android:text="FOB Revised2" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="160dp"
android:text="FOB Revised1" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="120dp"
android:text="Fuel on Board" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="1dp"
android:layout_y="0dp"
android:background="#BD3112"
android:text=" OPS Section "
android:textColor="#FFFFFF"
android:textSize="20dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="110dp"
android:ems="10" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="150dp"
android:ems="10" />
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="190dp"
android:ems="10" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="40dp"
android:text="Enter Trip Fuel" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="30dp"
android:ems="10" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="154dp"
android:layout_y="70dp"
android:ems="10" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="80dp"
android:text="RTOW" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="102dp"
android:layout_y="248dp"
android:background="#BD3112"
android:text=" Update "
android:textColor="#FFFFFF" />
</AbsoluteLayout>
Similar exception is being thrown on other activities as well, kindly let me know where I am going wrong... Thanks
Not all exceptions are under Exception class.change:
catch(Exception e)
To catch more exceptions types using Throwable class:
catch(Throwable e)

What should i do to move to another activity when only special conditions are met

I am making a quiz app. I want the exit page to be displayed when the next button is pressed after last question has been answered. I have a activity_questions.xml and a activity_exit.xml. The xml for activity_questions.xml is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.android.crystal.questions">
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:fontFamily="sans-serif-thin"
android:padding="10dp"
android:text=""
android:textColor="#440027"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text3"
android:layout_margin="30dp"
android:background="#9775AA"
android:hint="Answer"
android:inputType="text"
android:padding="10dp"
android:textSize="20dip" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edit2"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/answer"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onAnswerClick"
android:padding="2dp"
android:text="Okay" />
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onHintClick"
android:padding="2dp"
android:text="Hint" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="5dp"
android:orientation="vertical"
android:layout_marginTop="26dp"
android:layout_below="#+id/ll"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="#+id/tickcross"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/ll"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/wierdtick" />
<TextView
android:id="#+id/correctornot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tickcross"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Correct!"
android:textColor="#440027"
android:textSize="30dp" />
<Button
android:id="#+id/nextbutton"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Next"
android:onClick="onNextClick"
/>
</LinearLayout>
</RelativeLayout>
And the java is:
package com.example.android.crystal;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class questions extends AppCompatActivity {
Random r = new Random();
private boolean done;
private int QuestionNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
String[] questions = getResources().getStringArray(R.array.Questions);
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
}
public void onFinishClick(View view){
Intent intent = new Intent(this, exit.class);
startActivity(intent);
}
public static void hideKeyboardFrom(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
public void answersubmitted() {
findViewById(R.id.tickcross).setVisibility(View.VISIBLE);
TranslateAnimation animation = new TranslateAnimation(0, 0, 2000, 0);
animation.setDuration(1000);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
findViewById(R.id.tickcross).startAnimation(animation);
findViewById(R.id.correctornot).setVisibility(View.VISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.VISIBLE);
}
public void onHintClick(View view) {
String[] hints = getResources().getStringArray(R.array.Hints);
Toast toasty = Toast.makeText(getApplicationContext(), hints[QuestionNo], Toast.LENGTH_SHORT);
toasty.show();
}
public void onAnswerClick(View view) {
if (done == false) {
hideKeyboardFrom(this);
String answer = ((EditText) findViewById(R.id.edit2)).getText().toString();
String[] answers = getResources().getStringArray(R.array.Answers);
String correctAnswer = answers[QuestionNo];
correctAnswer = correctAnswer.toUpperCase();
answer = answer.toUpperCase();
if (answer.equals(correctAnswer)) {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("CORRECT!");
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.wierdtick));
answersubmitted();
} else {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("Correct Answer: " + correctAnswer);
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.weirdcross));
answersubmitted();
}
done = true;
}
}
public void onNextClick(View view){
if (done){
String[] questions = getResources().getStringArray(R.array.Questions);
if (QuestionNo < (questions.length - 1)) {
QuestionNo += 1;
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
EditText et = (EditText) findViewById(R.id.edit2);
et.setText("");
done = false;
}
else {
onFinishClick();
}
}
}
}
But the onFinishClick() gives an error
Thanks in advance for your help
What should i do to move to another activity when only special conditions are met
Basically, an if-statement and an Intent.
Raw code with no variables added:
if(condition(s)){
Intent i = new Intent(this, TargetActivity.class);
//Put arguments into the intent if you need them...
startActivity(i);
}
You can put it in a thread, in an onClickListener, anywhere and any way you want to execute the code.
As to the error you are receiving, I cannot help you with that until you supply the stacktrace from Logcat
You can do like this:
mNextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mQuestionCount < questionList.size()){
// set next question
} else {
// call finish
}
}
});

How to do enable a button and change its colour when textfield has some value?

I have tried many answers already given to this, but nothing seems to work properly. And the answers are only for changing state of the button, not colour and text. What am I missing?
I am a very VERY new learner to both Android and programming. And this is my first question on Stack Overflow. Hope it's as per guidelines.
I have a login page which looks like below (img1) [disabled][1]. If textfield has any value, the button should get enabled (img2) [enabled][2]. The Java and XML files are given below.
package io.kaapi.kaapimobileassistant.Activities;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;
import io.kaapi.kaapimobileassistant.Misc.StorageManager;
import io.kaapi.kaapimobileassistant.R;
public class LoginActivity extends AppCompatActivity {
private final static String TAG = "LoginActivity";
private Button login_button;
private TextInputLayout login_activation_layout;
private EditText login_activation_code;
private LinearLayout login_signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_button = (Button) findViewById(R.id.login_button);
login_activation_layout = (TextInputLayout) findViewById(R.id.login_acitivation_layout);
login_activation_code = (EditText) findViewById(R.id.login_activation_code);
login_signup = (LinearLayout) findViewById(R.id.login_signup);
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "Login pressed");
String activation_code_layout = login_activation_layout.getEditText().getText().toString();
Log.v(TAG, "Layout "+activation_code_layout);
String activation_code = login_activation_code.getText().toString();
Log.v(TAG, "Code "+activation_code);
if(activation_code.equalsIgnoreCase("")){
Log.v(TAG, "It's blank");
login_activation_layout.setError("Please enter an activation code");
} else {
Log.v(TAG, "Call login API, validate and show errors or login");
//StorageManager.write(LoginActivity.this, null, "client_domain", "http://ankit50.kaapi.io");
//StorageManager.write(LoginActivity.this, null, "client_logo", "http://cdn.kaapi.io/static");
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}
});
login_activation_code.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
enableSubmitIfReady();
}
#Override
public void afterTextChanged(Editable s) {
}
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setEnabled(false);
} else {
login_button.setEnabled(true);
}
}
});
login_signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("https://business.kaapi.io");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
The XML file is below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_vertical|center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="io.kaapi.kaapimobileassistant.Activities.LoginActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="71dp"
android:layout_marginRight="71dp"
android:layout_marginTop="66dp"
android:src="#drawable/kaapi_logo_login"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#color/colorTitle"
android:text="Activate Mobile Assistant"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="36dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:src="#drawable/ic_info_outline_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:textSize="14sp"
android:textStyle="italic"
android:layout_marginLeft="5dp"
android:text="The code was sent to you in sign up email and your web dashboard." />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/login_acitivation_layout"
android:layout_width="match_parent"
android:layout_marginTop="29dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/login_activation_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:maxLength="20"
android:hint="Activation code"
android:maxLines="1"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
style="#style/Widget.AppCompat.Button"
android:text="Activate"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="New to Kaapi? " />
<LinearLayout
android:id="#+id/login_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top"
android:text="Sign up first"
android:textColor="#color/colorPrimary"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
try this
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setClickable(false);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_light_green));// change color here so it's look like button disable
} else {
login_button.setClickable(true);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_dark_green));
}
}

Categories

Resources