I am trying to create a customized version of the ProgressDialog as it does not meet my current layout requirements.
I would like to render a vertically stacked ProgressDialog with the title centered on top of a centered spinner and the centered spinner on top of a message that typically receives updates on percentage of completion in a string format.
While trying to update the ProgressDialog's title and message, I am returned NullPointerException for the components.
How do I attempt to update the title and message of the progress dialog periodically ?
My code for the ProgressDialog.java class:
public class ProgressDialog extends AppCompatDialogFragment {
public volatile TextView progressDialogMessage = null;
public volatile TextView progressDialogTitle = null;
private View view = null;
private volatile AlertDialog aldialogInst = null;
private String title = "";
private String message = "";
public ProgressDialog() {
}
public ProgressDialog(String title, String message) {
this.title = title;
this.message = message;
}
@Override
public AlertDialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomAlertDialog);
LayoutInflater inflater = getActivity().getLayoutInflater();
view = inflater.inflate(R.layout.activity_progress_dialog, null);
progressDialogMessage = view.findViewById(R.id.progressDialogMessage);
progressDialogTitle = view.findViewById(R.id.progressDialogTitle);
System.out.println("[INF] ProgressDialog :: onCreate :: started ...");
if (progressDialogTitle == null) {
System.out.println("DIALOG TITLE IS NULL !!!");
}
builder.setView(view).setTitle(null);
progressDialogTitle.setText(this.title);
progressDialogMessage.setText(this.message);
aldialogInst = builder.create();
return aldialogInst;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
System.out.println("[INF] ProgressDialog :: onAttach :: started ...");
}
public void updateDialog(String title, @Nullable String message) {
progressDialogTitle.setText(title);
progressDialogMessage.setText(message);
progressDialogTitle.invalidate();
progressDialogMessage.invalidate();
}
public boolean isReady() {
if (aldialogInst != null) {
return true;
}
return false;
}
public AlertDialog getAlertDialog() {
return aldialogInst;
}
}
The XML for layout activity_progress_dialog.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">
<TextView
android:id="@+id/progressDialogTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:fontFamily="@font/lexend_deca_bold"
android:textAlignment="center"
android:textColor="@color/ventac_black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/progressDialogMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:fontFamily="@font/lexend_deca_light"
android:textAlignment="center"
android:textColor="@color/ventac_gray_1"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressDialogSpinner" />
<ProgressBar
android:id="@+id/progressDialogSpinner"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:indeterminateDrawable="@drawable/spinner"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressDialogTitle" />
<Space
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_marginStart="157dp"
android:layout_marginEnd="218dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressDialogMessage" />
</androidx.constraintlayout.widget.ConstraintLayout>
The MainActivity.java class:
package com.xxx.yyy.ui;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.work.Operation;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import android.app.ActivityManager;
import android.app.ApplicationExitInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.hardware.usb.UsbDevice;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.io.InputStream;
import java.security.Security;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private static Context myContext = null;
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myContext = this;
System.out.println("===== SHOWING CUSTOM NOTIFICATION DIALOG START =====");
ProgressDialog dialog = new ProgressDialog("Downloading Provenance", "0%");
dialog.show(((AppCompatActivity) myContext).getSupportFragmentManager(), "progdialog");
dialog.updateDialog("HELLO", "100%");
System.out.println("===== SHOWING CUSTOM NOTIFICATION DIALOG END =====");
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onStart() {
super.onStart();
}
}
The error message:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.watchlink.keepertag.ui.ProgressDialog.updateDialog(ProgressDialog.java:69)
at com.watchlink.keepertag.ui.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:8591)
at android.app.Activity.performCreate(Activity.java:8570)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4150)