无法与 OnClick\OnItemClickListener 中的视图及其事件进行交互

Can't interact with views and their events inside OnClick\OnItemClickListener

package com.example.android.miwok;

//all the imports

public class VocabularyManager extends Application{
    ProgressBar progress;
    ImageButton volup;
    ImageButton voldown;
    TextView current_vol_text;
    //[Cut-out Definitions of Variables]
    ImageButton.OnClickListener changevol;
    public VocabularyManager(Activity activity, Context context, String Category) {
        //Don't wonder, many things up here are also cut-out
        dialogview = inflater.inflate(R.layout.player_dialog, null);
        volup = dialogview.findViewById(R.id.vol_up); //MARKER #1
        voldown = dialogview.findViewById(R.id.vol_down); //MARKER #2
        progress = dialogview.findViewById(R.id.progress); //MARKER #3
        list = (ListView) this.act.findViewById(R.id.vocabulary_list);

        listadapter = new WordAdapter(this.act, mwords);

        list.setBackgroundColor(context.getResources().getColor(R.color.category_family));

        list.setAdapter(listadapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                list.setEnabled(false);
                Word word = mwords.get(i);
                timer = new Timer();
                dialog = new AlertDialog.Builder(mContext);
                dialogview = inflater.inflate(R.layout.player_dialog, null);
                volup.setOnClickListener(changevol);
                voldown.setOnClickListener(changevol);
                progress = dialogview.findViewById(R.id.progress);
                current_vol_text = dialogview.findViewById(R.id.current_stream_volume);
                current_vol_text.setText(Integer.toString(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)));
                //Setting up the 'dialog' AlertDialog.Builder
                crdialog = dialog.create();
                int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
                if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                    if(mMediaPlayer == null) {
                        mMediaPlayer = MediaPlayer.create(mContext, word.getAudio());
                        mMediaPlayer.start();
                        mMediaPlayer.setOnCompletionListener(mCompletionListener);
                    }
                }
                progress.setMax(mMediaPlayer.getDuration());
                progress.setProgress(0);
                crdialog.show();
                ttask = new TimerTask() {
                    @Override
                    public void run() {
                        if(mMediaPlayer != null) {
                            progress.setProgress(mMediaPlayer.getCurrentPosition());
                        }
                    }
                };
                timer.scheduleAtFixedRate(ttask, 0, 50);
            }
        });
        changevol = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(view.getId() == R.id.vol_up){
                    mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,0);
                }
                else{
                    mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,0);
                }
                current_vol_text.setText(Integer.toString(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)));
            }
        };
    }
}

带有此代码的探测器 is:I 正在尝试更改 OnClickListener(volchange) 内的 STREAM_MUSIC 音量,用于两个按钮 volupvoldown.出于某种原因,OnClickListener 未触发,但如果我打开 logcat,则不会显示任何错误、警告或可疑日志。是什么导致了这种行为?

As in the comments mentioned, the unnecessary parts of the code are cut-out for this post.

尝试删除图像按钮本地声明的 final 关键字

我认为您需要从 ImageButton 中删除 "final"。

要解决此问题,dialogview 膨胀后,在 OnItemClickListener 中初始化 volup/voldown - ImageButtons。 虽然widgets是在class构造函数中初始化的,但这一步还是需要做的。 应该对每个其他 view/widget 执行相同的过程,它可以通过调用相关方法在 UIThread 上更新。

为什么会出现这种行为?

理论上,我们在 class 构造函数中膨胀 dialogview 后正确初始化视图和小部件。 但是当我们在构造函数中膨胀 dialogview 时(在初始化 OnItemClickListener 之前),布局本身会膨胀,在调用构造函数之后 直接 ,然后 'binary'膨胀布局,然后我们得到视图。 但问题来了: 每次触发 OnItemClickListener 时,都会膨胀一个新的二进制布局并保存到 dialogview。所以现在应用到 dialog 的布局是从 dialogview-Variable 新创建的二进制布局,but volup & voldown ImageButtons 仍然引用之前创建的二进制布局,当我们曾经调用 class 构造函数时。 很明显,如果我们每次在创建 dialogview 后初始化 ImageButtons,按钮就会引用正确的、新的膨胀布局。