0

I have two classes: NewAppointment with activity xml file and GenerateTreatmentList which extends NewAppointment. In the xml I have a button which should bring forth a AlertDialog list which is implemented in GenerateTreatmentLis as follows:

import android.app.AlertDialog;
import android.content.Context;

import android.content.DialogInterface;


public class GenerateTreatmentList extends NewAppointment {

public void generateList(){

    final int[] listTitle = new int[1];
    final String[][] listCategories = new String[1][1];
    final int[] categoryArray = new int[1];

    listTitle[0] = R.string.title_category;
    categoryArray[0] = R.array.categories;
    listCategories[0] = getResources().getStringArray(categoryArray[0]);

    chooseTreatmentList(this, listTitle[0], listCategories[0], true, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                // Abonamente category
                case 0:
                {
                    listTitle[0] = R.string.title_abonament;
                    categoryArray[0] = R.array.abonamente;
                    listCategories[0] = getResources().getStringArray(categoryArray[0]);

                    chooseTreatmentList(context, listTitle[0], listCategories[0], true, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            switch (which) {
                                //Abonament - Vacuum corporal
                                case 0:
                                {
                                    listTitle[0] = R.string.title_timp;
                                    categoryArray[0] = R.array.ab_vacuum_time;
                                    listCategories[0] = getResources().getStringArray(categoryArray[0]);

                                    // Extract the treatment name
                                    treatmentChosen = R.string.vacuum_corp_name;

                                    chooseTreatmentList(context, listTitle[0], listCategories[0], true, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            // Extract treatment duration
                                            switch (which) {
                                                // Abonament - Vacuum corporal - 60 min
                                                case 0:
                                                {
                                                    treatmentDuration = R.string.name_60_min;


                                                } break;
                                                // Abonament - Vacuum corporal - 30 min
                                                case 1:
                                                {
                                                    treatmentDuration = R.string.name_30_min;
                                                } break;
                                                default: break;
                                            }

                                        }
                                    });
                                } break;
                                // Abonament - Ultrasunete
                                case 1:
                                {

                                } break;
                                //Abonament - Microdermoabraziune
                                case 2:
                                {

                                } break;
                                //Abonament - Electrostimulator
                                case 3:
                                {

                                } break;
                                default: break;
                            }

                        }
                    });
                } break;
                // Tratamente category
                case 1:
                {
                    listTitle[0] = R.string.title_tratament;
                    categoryArray[0] = R.array.tratamente;
                    listCategories[0] = getResources().getStringArray(categoryArray[0]);

                    chooseTreatmentList(context, listTitle[0], listCategories[0], true, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                } break;
                // Epilare category
                case 2:
                {
                    listTitle[0] = R.string.title_epilare;
                    categoryArray[0] = R.array.epilari;
                    listCategories[0] = getResources().getStringArray(categoryArray[0]);

                    chooseTreatmentList(context, listTitle[0], listCategories[0], true, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                } break;
                // Masaje category
                case 3:
                {
                    listTitle[0] = R.string.title_masaj;
                    categoryArray[0] = R.array.masaje;
                    listCategories[0] = getResources().getStringArray(categoryArray[0]);

                    chooseTreatmentList(context, listTitle[0], listCategories[0], true, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                } break;
                default:
                    break;
            }

        }
    });

}

private static void chooseTreatmentList(Context context, int listTitle,
                                        String[] listElements,
                                        boolean OnClickListener,
                                        DialogInterface.OnClickListener selectedItemListener) {

    AlertDialog.Builder treatmentList = new AlertDialog.Builder(context);
    treatmentList.setTitle(listTitle);
    treatmentList.setItems(listElements, selectedItemListener);
    treatmentList.create().show();
}
}

The Dialog list should open another dialog list each time and so on until a certain point (did not finish all the options yet). It works fine if I move all the code from GenerateTreatmentList to NewAppointment class, but it is something that I am trying to avoid because I will over flow the class.

When I press the button, the app restarts on the phone and I am getting the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
        at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
        at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
        at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
        at com.andygix.programarilucia.GenerateTreatmentList.generateList(GenerateTreatmentList.java:18)

Line 18:

listCategories[0] = getResources().getStringArray(categoryArray[0]);

At first I thought it was a missing context passing, but I tried a few methods and nothing worked. Unfortunatelly now I am stuck, but don't want to move this part to the NewAppointment class.

Any ideas?

Edit: Forgot to mention the call to the method from the NewAppointment class

GenerateTreatmentList getList = new GenerateTreatmentList();
getList.generateList();
3
  • You can't instantiate Activities, meaning you can't directly call methods on them. Commented Oct 11, 2018 at 14:40
  • Then any idea on how can I do this? I was thinking of making an activity for the GenerateTreatmentList class and create a custom list dialog, but I'm not sure if it will work and would like a second (or third) opinion.
    – Andy Gix
    Commented Oct 11, 2018 at 21:27
  • I don't really even know what you're trying to do. Commented Oct 11, 2018 at 21:33

1 Answer 1

0

Seems that I managed to find how to resolve the issue. I made the following changes:

In GenerateTreatmentList class I created a method with the class name and context as parameter.

private Context context;

    public GenerateTreatmentList (Context context) {
        this.context = context;
    }

Then, when trying to access a resource I also added the context before the path to the given string or array list

listCategories[0] = context.getResources().getStringArray(categoryArray[0]);

In the NewAppointment class, when trying to call the the non-activity one, I passed the context:

   public void chooseTreatment() {
        GenerateTreatmentList getList = new GenerateTreatmentList(this);
        getList.generateList();
   }

I know that my code looks sloppy and that's because I'm new to this thing. I usually write it in an expanded way, see if it works than try to simplify it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.