为什么无法与OnClick \ OnItemClickListener中的视图及其事件交互?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (185)
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)));
            }
        };
    }
}

具有此代码STREAM_MUSIC是:我正在尝试volchange为两个按钮volup和两个按钮更改OnClickListener()中的Volume voldown。出于某种原因,OnClickListener不会触发,但是如果我打开logcat,则不会显示任何错误,警告或可疑日志。什么导致这种行为?

提问于
用户回答回答于

我认为你需要从ImageButton中删除“final”。

用户回答回答于

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

扫码关注云+社区

领取腾讯云代金券