繁体   English   中英

错误:无法从非活动类调用方法-生成NullPointerException错误

[英]Error: Can't get to call method from non-activity class - generates NullPointerException error

我有两个类:具有活动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类中,我创建了一个以类名和上下文为参数的方法。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM