所以我正在创建一个Android音板应用程序,并且当我点击音板应用程序的最后一个按钮时,我的应用程序崩溃了。
[代码]
public class newBoard extends Activity {
int selectedSoundId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer player = new MediaPlayer();
final Resources res = getResources();
// just keep them in the same order, e.g. button01 is tied to backtoyou
final int[] buttonIds = { R.id.button1, R.id.button2, R.id.button3,
R.id.button4, R.id.button5, R.id.button6, R.id.button7,
R.id.button8, R.id.button9, R.id.button10, R.id.button11,
R.id.button12, R.id.button13, R.id.button14, R.id.button15,
R.id.button16, R.id.button16, R.id.button17, R.id.button18,
R.id.button19, R.id.button20, R.id.button21, R.id.button22,
R.id.button23, R.id.button24, R.id.button25, R.id.button26,
R.id.button27, R.id.button28, R.id.button29, R.id.button30,
R.id.button31, R.id.button32 };
final int[] soundIds = { R.raw.bengalka, R.raw.cista_psihologija,
R.raw.da_ne, R.raw.dejo_narkomane, R.raw.dizi_se,
R.raw.fejslifting, R.raw.fotomale, R.raw.gladan_sam,
R.raw.jasna_pero, R.raw.jeben_vam_mater, R.raw.kae_ivanisevic,
R.raw.kae_to_fora, R.raw.kaj_gledas, R.raw.kaj_vi_gledate,
R.raw.kineza_crnaca, R.raw.kozo_nepodojena, R.raw.marino,
R.raw.mater_zbrgljavu, R.raw.muha, R.raw.nema_papira,
R.raw.nered, R.raw.ne_spominji_majku, R.raw.nisam_se_uroko,
R.raw.odfurati_doktoru, R.raw.pljacka, R.raw.pusi_ke,
R.raw.sava_sava, R.raw.tebe_i_magazin, R.raw.tog_vani_nema,
R.raw.za_dom_spremni, R.raw.zrigati };
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
// find the index that matches the button's ID, and then reset
// the MediaPlayer instance, set the data source to the
// corresponding
// sound effect, prepare it, and start it playing.
for (int i = 0; i < buttonIds.length; i++) {
if (v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res
.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
break;
}
}
}
};
// set the same listener for every button ID, no need
// to keep a reference to every button
for (int i = 0; i < buttonIds.length; i++) {
Button soundButton = (Button) findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener);
}
}
}
此行显示例外: selectedSoundId = soundIds [i];
selectedSoundId = soundIds[i];
答案 0 :(得分:2)
Off-by-two:有33个按钮ID和31个声音ID。按钮16是重复的。
要映射资源ID和其他整数,请考虑使用地图,例如SparseIntArray
。
答案 1 :(得分:2)
final int[] buttonIds = 33 Value - you have two R.id.button16 in your buttonIds
final int[] soundIds = 31 Value
所以它崩溃了。