notification channels app crashes and stops - java

this is my code:
package com.rockykhan.notificationchannels;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public class app extends Application {
// MAKING CHANNEL ID'S AS FINAL STRINGS
public static final String CHANNEL_1_ID = "channel1";
public static final String CHANNEL_2_ID = "channel2";
#Override
public void onCreate() {
super.onCreate();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID, "Channel 1", NotificationManager.IMPORTANCE_HIGH);
NotificationChannel channel2 = new NotificationChannel(CHANNEL_2_ID, "Channel 2", NotificationManager.IMPORTANCE_LOW);
channel1.setDescription("this is channel 1");
channel2.setDescription("this is channel 2");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
manager.createNotificationChannel(channel2);
}
}
}
mainActivity:
package com.rockykhan.notificationchannels;
import static com.rockykhan.notificationchannels.app.CHANNEL_1_ID;
import static com.rockykhan.notificationchannels.app.CHANNEL_2_ID;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.Notification;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText title, text;
Button btn_c1, btn_c2;
private NotificationManagerCompat notificationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title.findViewById(R.id.notificationTitleText);
text.findViewById(R.id.notificationMessageText);
btn_c1.findViewById(R.id.sendNotificationThroughChannel1);
btn_c2.findViewById(R.id.sendNotificationThroughChannel2);
String notificationTitle = title.getText().toString();
String notificationText = text.getText().toString();
notificationManager = NotificationManagerCompat.from(this);
btn_c1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Notification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.imp_noti)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.build();
notificationManager.notify(1, notification);
}
});
btn_c2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Notification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_2_ID)
.setSmallIcon(R.drawable.noti)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.build();
notificationManager.notify(2, notification);
}
});
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/sendNotificationThroughChannel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send on channel 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/notificationTitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Title"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintBottom_toTopOf="#+id/sendNotificationThroughChannel2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/sendNotificationThroughChannel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send on channel 1"
app:layout_constraintBottom_toTopOf="#+id/sendNotificationThroughChannel2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/notificationMessageText" />
<EditText
android:id="#+id/notificationMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintBottom_toTopOf="#+id/sendNotificationThroughChannel1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/notificationTitleText" />
</androidx.constraintlayout.widget.ConstraintLayout>
logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rockykhan.notificationchannels/com.rockykhan.notificationchannels.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.EditText.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3408)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3547)
So I was trying to send notifications through buttons but the app doesn't seem to start. It crashes and as far as I thought, it was a problem in xml Id's but I can't find the main problem here ( I think it is something to do with the findViewbyId of the editText). Help will be much appreciated!

Assign the result of calling findViewById() to your View variables, don't call findViewById() on the variables itself.
So basically replace these lines
title.findViewById(R.id.notificationTitleText);
text.findViewById(R.id.notificationMessageText);
btn_c1.findViewById(R.id.sendNotificationThroughChannel1);
btn_c2.findViewById(R.id.sendNotificationThroughChannel2);
with these
title = findViewById(R.id.notificationTitleText);
text = findViewById(R.id.notificationMessageText);
btn_c1 = findViewById(R.id.sendNotificationThroughChannel1);
btn_c2 = findViewById(R.id.sendNotificationThroughChannel2);

Related

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

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

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

Notifications don't show [duplicate]

This question already has an answer here:
Notifications don't appear
(1 answer)
Closed 2 years ago.
I'm new to programming and app development and I'm trying to develop an app that shows events and notifies the user when a new event is up.I'm trying to create a settings activity with 2 switch buttons, 1 for notifications on status bar and 1 for notification sound. When I turn on the switches, I don't get any notification nor sound.
How can I fix that?
JAVA FILE
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
public class Settings extends AppCompatActivity {
Switch simpleSwitch1, simpleSwitch2;
NotificationManager manager;
Notification myNotication;
private Notification notification;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
if (simpleSwitch1.isChecked()){
simpleSwitch1.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onClick(View arg0) {
//API level 11
Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");
PendingIntent pendingIntent = PendingIntent.getActivity(Settings.this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(Settings.this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.drawable.notification);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext..."); //API level 16
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);
/*
//API level 8
Notification myNotification8 = new Notification(R.drawable.ic_launcher, "this is ticker text 8", System.currentTimeMillis());
Intent intent2 = new Intent(MainActivity.this, SecActivity.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 2, intent2, 0);
myNotification8.setLatestEventInfo(getApplicationContext(), "API level 8", "this is api 8 msg", pendingIntent2);
manager.notify(11, myNotification8);
*/
}
});}
else{
simpleSwitch1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
manager.cancel(11);
}
});
}
}
public void onClick(View v) {
simpleSwitch2 = (Switch) findViewById(R.id.simpleswitch2);
String statusSwitch2;
if (simpleSwitch2.isChecked())
notification.defaults |= Notification.DEFAULT_SOUND;
}}
XML FILE
<?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=".Settings">
<TextView
android:id="#+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="SETTINGS"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Headline"
android:textColor="#color/design_default_color_primary_dark"
android:textColorHighlight="#color/design_default_color_primary"
android:textStyle="bold" />
<Switch
android:id="#+id/simpleswitch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView16"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:text="NOTIFICATIONS"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#color/colorPrimary"
android:textStyle="normal"
android:checked="true"
/>
<Switch
android:id="#+id/simpleswitch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/simpleswitch1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="SOUNDS"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#color/colorPrimary"
android:checked="true" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="61dp"
android:layout_height="44dp"
android:layout_marginLeft="280dp"
android:layout_marginTop="30dp"
app:srcCompat="#drawable/ic_menu_manage" />
</RelativeLayout>
A couple of stuff here:
For switches, you probably want OnCheckedChangeListener, not
OnClickListener.
You want to set the OnCheckedChangeListener once in your onCreate() and check for the Switch's state
inside this listener:
simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
simpleSwitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// show notification
} else {
// cancel notification
}
}
});`
// Similarly for switch2`

Android Studio Attempt to invoke virtual method on a null object reference error [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I have problem with LoginNew activity. When I click "Zaloguj się" button (id LogIn) app stops working.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference error is displaying.
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zapp4, PID: 23951
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.zapp4.LoginNew.userLogin(LoginNew.java:61)
at com.example.zapp4.LoginNew.onClick(LoginNew.java:120)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Process 23951 terminated.
LoginNew.java code:
package com.example.zapp4;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
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.FirebaseAuthUserCollisionException;
public class LoginNew extends AppCompatActivity implements View.OnClickListener {
FirebaseAuth mAuth;
EditText EditTextUsername;
EditText EditTextPassword;
ProgressBar progressBar;
Button Przyciskzaloguj;
ImageView zcoinimage;
TextView zcoinnumber;
CardView LogIn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_new);
mAuth = FirebaseAuth.getInstance();
EditTextUsername = (EditText) findViewById(R.id.EditTextUsernameedit);
EditTextPassword = (EditText) findViewById(R.id.EditTextPasswordedit);
Przyciskzaloguj = (Button) findViewById(R.id.Przyciskzaloguj);
zcoinnumber = (TextView) findViewById(R.id.zcoinnumber);
zcoinimage = (ImageView) findViewById(R.id.zcoinimage);
progressBar = findViewById(R.id.progressBar);
LogIn = findViewById(R.id.LogIn);
findViewById(R.id.LogIn).setOnClickListener(this);
}
private void userLogin() {
String username = EditTextUsername.getText().toString();
String password = EditTextPassword.getText().toString().trim();
if (username.isEmpty()) {
EditTextUsername.setError("Email jest wymagany");
EditTextUsername.requestFocus();
return;
}
if (password.isEmpty()) {
EditTextPassword.setError("Hasło jest wymagane");
EditTextPassword.requestFocus();
return;
}
if (password.length() < 6) {
EditTextPassword.setError("Minimalna długość hasła wynosi 6");
EditTextPassword.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(username).matches()) {
EditTextUsername.setError("Wprowadź poprawny email");
EditTextUsername.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.signInWithEmailAndPassword(username, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
finish();
Intent intent = new Intent(LoginNew.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Przyciskzaloguj.setVisibility(View.GONE);
zcoinnumber.setVisibility(View.VISIBLE);
zcoinimage.setVisibility(View.VISIBLE);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, HomeActivity.class));
}
}
#Override
public void onClick(View view) {
if (view.getId() == R.id.LogIn) {
userLogin();
}
}
}
activity_login_new.xml code:
<?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"
android:background="#drawable/gradient"
tools:context=".LoginNew">
<ImageView
android:id="#+id/imageView"
android:layout_width="233dp"
android:layout_height="297dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="88dp"
android:layout_marginTop="47dp"
android:layout_marginEnd="88dp"
android:layout_marginBottom="387dp"
android:contentDescription="#string/todo"
app:srcCompat="#drawable/znaczek_druzyny" />
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/EditTextUsernameedit"
android:layout_width="250dp"
android:layout_height="40dp"
android:background="#11000000"
android:hint="#string/prompt_email"
android:inputType="textPersonName"
android:textSize="16sp"
android:layout_marginBottom="35dp"
android:layout_above="#+id/EditTextPasswordedit"
android:layout_centerHorizontal="true"
android:autofillHints="" />
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/EditTextPasswordedit"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_alignStart="#+id/EditTextUsernameedit"
android:layout_alignParentBottom="true"
android:layout_marginStart="2dp"
android:layout_marginBottom="201dp"
android:background="#11000000"
android:textSize="16sp"
android:hint="#string/prompt_password"
android:inputType="textPassword"
android:autofillHints="" />
<androidx.cardview.widget.CardView
android:id="#+id/LogIn"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="55dp"
android:layout_marginEnd="55dp"
android:layout_marginBottom="85dp"
app:cardBackgroundColor="#color/colorAccent"
app:cardCornerRadius="25dp"
app:cardElevation="10dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="13dp"
android:layout_marginBottom="13dp"
android:text="#string/action_sign_in"
android:textColor="#fff"
android:textSize="30sp" />
</androidx.cardview.widget.CardView>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="265dp"
android:layout_height="265dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="65dp"
android:layout_marginTop="230dp"
android:layout_marginEnd="65dp"
android:layout_marginBottom="230dp"
android:visibility="gone" />
</RelativeLayout>
Change
EditText EditTextUsername;
EditText EditTextPassword;
To
AppCompatEditText EditTextUsername;
AppCompatEditText EditTextPassword;
And try
First, you have to import androidx.appcompat.widget.AppCompatEditText instead of android.widget.EditText
Secondly, use AppCompatEditText instead of EditText. you should modify the following lines
FirebaseAuth mAuth;
AppCompatEditText EditTextUsername;
AppCompatEditText EditTextPassword;
...
hope it helps you.

Crash app when adding multiple activities android studio

Hello everybody I got an issue when I trying to run my app in my physical phone and just get crashed at run. its my 5 time making this app, when this happen just close at installing the app. I'm trying to make an app in the main activity two Image buttons and then open anther activity, each one, I checked myself without creating the image buttons. but when I putted the image button the app just get crashed.
here is my code.
ActivityMain.xml
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="#drawable/blurred"
tools:context=".MainActivity">
<ImageView
android:id="#+id/blurbackground"
android:layout_width="match_parent"
android:layout_height="97dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/backmain" />
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="18dp"
android:layout_marginLeft="18dp"
android:layout_marginTop="62dp"
android:text="Welcome!"
android:textColor="#android:color/white"
android:textSize="22sp" />
<TextView
android:id="#+id/txt2"
android:layout_width="345dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="7dp"
android:layout_marginTop="114dp"
android:gravity="center"
android:text="Master all professions in the professions guide"
android:textColor="#android:color/white"
android:textColorHighlight="#android:color/black"
android:textSize="18sp"
android:typeface="sans" />
<TextView
android:id="#+id/txt4"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginBottom="348dp"
android:textColorHighlight="#android:color/black"
android:text="Be part of the game and be the master of professions, these guides are collected from users who offer great help in your adventure throughout Azeroth!"
android:textColor="#android:color/white"
android:textStyle="italic"
android:typeface="sans" />
<TextView
android:id="#+id/txt7"
android:layout_width="322dp"
android:layout_height="64dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginBottom="75dp"
android:text="We have classroom guides available for you to master your character if you are fresh and new to the world of Warcraft, dominate your enemies and take the victory!"
android:textColor="#android:color/white"
android:textColorHighlight="#android:color/black" />
<ImageView
android:id="#+id/imageView"
android:layout_width="227dp"
android:layout_height="103dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="66dp"
android:layout_marginLeft="66dp"
android:layout_marginTop="223dp"
app:srcCompat="#drawable/alchem"
tools:scaleType="centerCrop" />
<ImageButton
android:id="#+id/buthorde"
android:layout_width="68dp"
android:layout_height="75dp"
android:layout_alignStart="#+id/imageView"
android:layout_alignParentBottom="true"
android:layout_marginStart="3dp"
android:layout_marginBottom="155dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/horde" />
<ImageButton
android:id="#+id/butally"
android:layout_width="70dp"
android:layout_height="73dp"
android:layout_alignEnd="#+id/imageView"
android:layout_alignBottom="#+id/buthorde"
android:layout_marginEnd="5dp"
android:layout_marginBottom="3dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/ally" />
<Button
android:id="#+id/butshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="21dp"
android:text="Show me!"
android:textAllCaps="false"
android:textStyle="italic"
android:typeface="sans" />
ActivityMain.java
package hordemuzzle.wowprofessions;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button butonhorde;
private Button butonally;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--------------------------------------------------------------------------- -------------------------
TextView textView = (TextView) findViewById(R.id.txt1);
textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView.setText("Welcome!");
TextView textView1 = (TextView) findViewById(R.id.txt2);
textView1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView1.setText("Master all professions in the professions guide");
//----------------------------------------------------------------------------------------------------
butonally=(Button) findViewById(R.id.butally);
butonally.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActally();
}
});
butonhorde=(Button) findViewById(R.id.buthorde);
butonhorde.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActhorde();
}
});
}
public void openActhorde(){
Intent intenth = new Intent(this, Activityhorde.class);
startActivity(intenth);
}
public void openActally(){
Intent intenta = new Intent(this, Activityally.class);
startActivity(intenta);
}
}
Logcat
2018-11-01 23:30:05.204 1051-19358/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
2018-11-01 23:30:05.204 1051-19358/system_process D/OpenGLRenderer: Swap behavior 1
2018-11-01 23:30:05.291 1051-12094/system_process D/GraphicsStats: Buffer count: 4
2018-11-01 23:30:05.291 1051-4361/system_process I/WindowManager: WIN DEATH: Window{3382c8d u0 hordemuzzle.wowprofessions/hordemuzzle.wowprofessions.SplashScreenActivity}
2018-11-01 23:30:05.291 1051-12088/system_process I/ActivityManager: Process hordemuzzle.wowprofessions (pid 24362) has died
2018-11-01 23:30:05.291 1051-12088/system_process D/ActivityManager: cleanUpApplicationRecord -- 24362
Splash screen activity
package hordemuzzle.wowprofessions;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import gr.net.maroulis.library.EasySplashScreen;
import static android.support.v4.os.LocaleListCompat.create;
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EasySplashScreen config = new EasySplashScreen(SplashScreenActivity.this)
.withFullScreen()
.withTargetActivity(MainActivity.class)
.withSplashTimeOut(4000)
.withBackgroundResource(android.R.color.background_dark)
.withLogo(R.drawable.logo);
View easysplashscreen = config.create();
setContentView(easysplashscreen);
}
}
try this
package hordemuzzle.wowprofessions;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--------------------------------------------------------------------------- -------------------------
TextView textView = (TextView) findViewById(R.id.txt1);
textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView.setText("Welcome!");
TextView textView1 = (TextView) findViewById(R.id.txt2);
textView1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView1.setText("Master all professions in the professions guide");
//----------------------------------------------------------------------------------------------------
((ImageButton) findViewById(R.id.butally)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActally();
}
});
((ImageButton) findViewById(R.id.buthorde)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActhorde();
}
});
}
public void openActhorde(){
Intent intenth = new Intent(this, Activityhorde.class);
startActivity(intenth);
}
public void openActally(){
Intent intenta = new Intent(this, Activityally.class);
startActivity(intenta);
}
}

Categories

Resources