首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法与OnClick\OnItemClickListener中的视图及其事件交互

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

Stack Overflow用户
提问于 2018-05-27 01:43:24
回答 3查看 47关注 0票数 -1
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)));
            }
        };
    }
}

这段代码的问题是:我正在尝试为两个按钮volupvoldown更改OnClickListener(volchange)中的STREAM_MUSIC音量。由于某些原因,OnClickListener无法触发,但如果我打开日志,则不会显示任何错误、警告或可疑日志。是什么导致了这种行为?

正如在评论中提到的,这篇文章中不必要的代码部分被删掉了。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50545482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档