Android:声音应用程序在更改背景颜色时崩溃

时间:2013-03-04 14:26:14

标签: android audio background onclick

我有以下问题。我有3个按钮用于更改背景颜色(分别为蓝色,红色和绿色)以及5个单击时会发出声音的单选按钮。背景颜色更改按钮以及声音单选按钮都可以独立工作。只要我将它们放在一个屏幕上,当选择了一个背景颜色更改按钮时,应用程序就会崩溃。问题在于RadioButton系列,特别是在这里:

RadioButton rb = (RadioButton) v;

我不知道如何对付这个错误。有人能帮助我吗?

以下是onClick方法的代码:

               public void onClick(View v) {


            RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelativeLayout1);

               switch (v.getId()) 
               {
               case R.id.button1:
                   Toast.makeText(getBaseContext(), "You switched the color to red!" , Toast.LENGTH_SHORT ).show();
                   rl.setBackgroundColor(Color.RED);

               break;
               case R.id.button2:
                   Toast.makeText(getBaseContext(), "You switched the color to green!" , Toast.LENGTH_SHORT ).show();
                   rl.setBackgroundColor(Color.GREEN);
               break;
               case R.id.button3:
                   Toast.makeText(getBaseContext(), "You switched the color to blue" , Toast.LENGTH_SHORT ).show();
                   rl.setBackgroundColor(Color.BLUE);
               break;
               case R.id.cancel:
                   Toast.makeText(getBaseContext(), "This application will be closed!" , Toast.LENGTH_SHORT ).show();
                   finish();
               break;
               }


               //Perform action on clicks
                 RadioButton rb = (RadioButton) v;  

                 //get the user sound settings
                 AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

                 //get current volume
                 float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

                 //get maximum volume
                 float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

                 float volume = actualVolume / maxVolume;

             //is the sound loaded already?
                 if(loaded) {
                     //play the sound
                     if (rb.getText().toString().equals("Clong!"))
                         gameAudio.play(soundIDs[0], volume, volume, 1, 0, 1f);
                     else if (rb.getText().toString().equals("Hammering!"))
                         gameAudio.play(soundIDs[1], volume, volume, 1, 0, 1f);
                     else if (rb.getText().toString().equals("Chainsaw Attack!"))
                         gameAudio.play(soundIDs[2], volume, volume, 1, 0, 1f);
                     else if (rb.getText().toString().equals("Smoke alarm!"))
                         gameAudio.play(soundIDs[3], volume, volume, 1, 0, 1f);
                     else if (rb.getText().toString().equals("Sharp the knife!"))
                         gameAudio.play(soundIDs[4], volume, volume, 1, 0, 1f);
                 }

           }

2 个答案:

答案 0 :(得分:0)

听起来你的onClick()被按下按钮被触发......在这种情况下,View v不是RadioButton而是按钮。

在这种情况下,您的logcat(您应该始终包含在此处)将显示ClassCastException。

将整个第二部分(处理声音)包裹在

if( v instanceof RadioButton ) {
    ... your code here
}

你应该没事。

答案 1 :(得分:0)

试试这个

//Perform action on clicks
if(v instanceof RadioButton)
{
                 RadioButton rb = (RadioButton) v;  
}else return;