对话框中 Spinner Adapter 中的空指针异常

Null Pointer Exception in Spinner Adapter within dialog

当我尝试在 android studio 中为微调器创建适配器时,出现空指针异常。 我做了一个对话框,微调器在自定义对话框中,所以我不知道如何创建它。

这是onCreate()里面对话框的代码

fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            Spinner spinner = findViewById(R.id.spinner_DogType);

            ArrayAdapter<String> adapter = new ArrayAdapter<>(HomeActivity.this,R.layout.custom_spinner,getResources().getStringArray(R.array.DogTypes));

            adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown);
            spinner.setAdapter(adapter);

            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    openFileChooser();
                }
            });

            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    addDog();
                }
            });

            dialog.show();
        }
    });

这是我得到的错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
        at com.niko.finalapp.HomeActivity.onClick(HomeActivity.java:117)
        at android.view.View.performClick(View.java:7201)
        at android.view.View.performClickInternal(View.java:7170)
        at android.view.View.access00(View.java:806)
        at android.view.View$PerformClick.run(View.java:27582)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7695)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

第117行是这样的:

spinner.setAdapter(适配器);

您必须首先通过对话框视图中的 id 查找视图来扩充自定义对话框视图:

    final Dialog dialog = new Dialog(CurrentActivity.this, R.style.CustomDialogTheme);
    View view = LayoutInflater.from(CurrentActivity.this).inflate(R.layout.dialog_clear_history, null);

    dialog.setContentView(view);


    Spinner spinner = dialog.findViewById(R.id.spinner_DogType);

    ArrayAdapter<String> adapter = new ArrayAdapter<>(CurrentActivity.this,R.layout.custom_spinner,getResources().getStringArray(R.array.DogTypes));

    adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown);
    spinner.setAdapter(adapter);

    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openFileChooser();
        }
    });

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addDog();
        }
    });

    dialog.show();

我把这段代码放在上面

fab = findViewById(R.id.flot);

    dialog = new Dialog(HomeActivity.this);
    dialog.setContentView(R.layout.add_item);