0

我有以下问题。我有 3 个按钮用于更改背景颜色(分别为蓝色、红色和绿色),还有 5 个单选按钮,单击时会播放声音。背景颜色更改按钮和声音单选按钮都可以独立工作。As soon as I put them together on one screen, the application will crash when one of the background color change button is selected. 问题在于 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);
                 }

           }
4

2 回答 2

0

尝试这个

//Perform action on clicks
if(v instanceof RadioButton)
{
                 RadioButton rb = (RadioButton) v;  
}else return;
于 2013-03-04T14:32:52.563 回答
0

听起来你onClick()是因为按下按钮而被解雇......在这种情况下,这View v不是RadioButton一个按钮而是一个​​按钮。

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

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

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

你应该没事。

于 2013-03-04T14:31:26.950 回答