1

我有一个 AsyncTask ,其中 onPostExecute 方法是:

protected void onPostExecute(String args) {


            final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.dialog_root_element));


            AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
            builder.setView(layout)
                    .setMessage(name)
                    .setTitle("playing...")
                    .setCancelable(false)
                    .setNeutralButton("stop/volver",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    mediaPlayer.stop();
                                    dialog.cancel();
                                }
                            });
            alert = builder.create();

            alert.show();

            SeekBar volControl = (SeekBar)findViewById(R.id.dialog_seekbar);
            volControl.setMax(maxVolume);
            volControl.setProgress(curVolume);
            volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onStopTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
                }
            });


            mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    mediaPlayer.stop();
                    alert.dismiss();
                }
            });

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                }
            });

        }

错误在于:

volControl.setMax(maxVolume);

但我不明白为什么......(在调试模式下,maxVolume 为 15)

我正在尝试使用音量控制 seekBar 进行对话,但没有看到错误。我可以用 AsyncTask 修改声音的音量吗?

多谢!

4

1 回答 1

1

你调用findViewById了错误的对象。该代码正在您SeekBar的活动的布局文件中查找您的文件,该文件可能不存在(我猜您遇到了空指针异常)。

试试这个,显示你的对话框,然后调用findViewById对话框而不是找到你的SeekBar.

alert.show();
SeekBar volControl = (SeekBar)alert.findViewById(R.id.dialog_seekbar);

这里的重要部分是alert.findViewById()

于 2012-07-11T16:52:43.390 回答