【问题标题】:Error: Can't get to call method from non-activity class - generates NullPointerException error错误:无法从非活动类调用方法 - 生成 NullPointerException 错误
【发布时间】:2018-10-11 14:37:26
【问题描述】:

我有两个类:带有活动 xml 文件的 NewAppointment 和扩展 NewAppointment 的 GenerateTreatmentList。 在 xml 中,我有一个按钮,它应该带来一个 AlertDialog 列表,该列表在 GenerateTreatmentLis 中实现,如下所示:

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

对话框列表每次都应该打开另一个对话框列表,依此类推,直到某个点(尚未完成所有选项)。如果我将所有代码从 GenerateTreatmentList 移动到 NewAppointment 类,它工作正常,但这是我试图避免的事情,因为我会溢出类。

当我按下按钮时,应用程序在手机上重新启动,我收到以下错误:

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 类中。

有什么想法吗?

编辑:忘记提及从 NewAppointment 类调用方法

GenerateTreatmentList getList = new GenerateTreatmentList();
getList.generateList();

【问题讨论】:

  • 你不能实例化活动,这意味着你不能直接调用它们的方法。
  • 那么我该怎么做呢?我正在考虑为 GenerateTreatmentList 类创建一个活动并创建一个自定义列表对话框,但我不确定它是否会起作用并且想要第二(或第三)意见。
  • 我什至不知道你想做什么。

标签: java android nullpointerexception android-context


【解决方案1】:

似乎我设法找到了解决问题的方法。我做了以下更改:

GenerateTreatmentList 类中,我创建了一个以类名和上下文作为参数的方法。

private Context context;

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

然后,在尝试访问资源时,我还在给定字符串或数组列表的路径之前添加了上下文

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

在 NewAppointment 类中,当尝试调用非活动类时,我传递了上下文:

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

我知道我的代码看起来很草率,那是因为我是新手。我通常用扩展的方式写它,看看它是否有效而不是尝试简化它。

【讨论】:

    猜你喜欢
    • 2015-12-23
    • 1970-01-01
    • 2014-08-29
    • 2021-07-17
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多