Error: Can't get to call method from non-activity class - generates NullPointerException error
Error: Can't get to call method from non-activity class - generates NullPointerException error
我有两个 classes:带有 activity xml 文件的 NewAppointment 和扩展 NewAppointment 的 GenerateTreatmentList。
在 xml 中,我有一个按钮,它应该显示一个在 GenerateTreatmentLis 中实现的 AlertDialog 列表,如下所示:
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();
}
}
Dialog list每次都要打开另一个dialog list,以此类推,直到某个点(还没有完成所有选项)。如果我将所有代码从 GenerateTreatmentList 移动到 NewAppointment class,它工作正常,但这是我试图避免的事情,因为我会溢出 class.
当我按下按钮时,应用程序在 phone 上重新启动,我收到以下错误:
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)
第 18 行:
listCategories[0] = getResources().getStringArray(categoryArray[0]);
起初我以为是缺少上下文传递,但是我尝试了几种方法都没有用。不幸的是,现在我被卡住了,但不想将这部分移动到 NewAppointment class。
有什么想法吗?
编辑:忘记提及从 NewAppointment 调用方法 class
GenerateTreatmentList getList = new GenerateTreatmentList();
getList.generateList();
我似乎找到了解决问题的方法。我做了以下更改:
在 GenerateTreatmentList
class 中,我创建了一个使用 class 名称和上下文作为参数的方法。
private Context context;
public GenerateTreatmentList (Context context) {
this.context = context;
}
然后,在尝试访问资源时,我还在给定字符串或数组列表的路径之前添加了上下文
listCategories[0] = context.getResources().getStringArray(categoryArray[0]);
在 NewAppointment class 中,当尝试调用非 activity 时,我传递了上下文:
public void chooseTreatment() {
GenerateTreatmentList getList = new GenerateTreatmentList(this);
getList.generateList();
}
我知道我的代码看起来很草率,那是因为我是新手。我一般都是扩展写的,看看能不能行,再简化一下。
我有两个 classes:带有 activity xml 文件的 NewAppointment 和扩展 NewAppointment 的 GenerateTreatmentList。 在 xml 中,我有一个按钮,它应该显示一个在 GenerateTreatmentLis 中实现的 AlertDialog 列表,如下所示:
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();
}
}
Dialog list每次都要打开另一个dialog list,以此类推,直到某个点(还没有完成所有选项)。如果我将所有代码从 GenerateTreatmentList 移动到 NewAppointment class,它工作正常,但这是我试图避免的事情,因为我会溢出 class.
当我按下按钮时,应用程序在 phone 上重新启动,我收到以下错误:
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)
第 18 行:
listCategories[0] = getResources().getStringArray(categoryArray[0]);
起初我以为是缺少上下文传递,但是我尝试了几种方法都没有用。不幸的是,现在我被卡住了,但不想将这部分移动到 NewAppointment class。
有什么想法吗?
编辑:忘记提及从 NewAppointment 调用方法 class
GenerateTreatmentList getList = new GenerateTreatmentList();
getList.generateList();
我似乎找到了解决问题的方法。我做了以下更改:
在 GenerateTreatmentList
class 中,我创建了一个使用 class 名称和上下文作为参数的方法。
private Context context;
public GenerateTreatmentList (Context context) {
this.context = context;
}
然后,在尝试访问资源时,我还在给定字符串或数组列表的路径之前添加了上下文
listCategories[0] = context.getResources().getStringArray(categoryArray[0]);
在 NewAppointment class 中,当尝试调用非 activity 时,我传递了上下文:
public void chooseTreatment() {
GenerateTreatmentList getList = new GenerateTreatmentList(this);
getList.generateList();
}
我知道我的代码看起来很草率,那是因为我是新手。我一般都是扩展写的,看看能不能行,再简化一下。