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