The problem that I am having is that when these buttons are pressed too much, the application crashes. I can post a logcat of the issue, but I would like someone to analyze the code (which is very simplistic) and tell me if there is a better way of doing this.
Using java Syntax Highlighting
- package com.drum.nation;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Debug;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.*;
- import android.media.MediaPlayer;
- public class DrumNation extends Activity {
- /** Called when the activity is first created. */
- MediaPlayer padSound;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button pad1 = (Button) findViewById(R.id.pad1);
- Button pad2 = (Button) findViewById(R.id.pad2);
- Button pad3 = (Button) findViewById(R.id.pad3);
- Button pad4 = (Button) findViewById(R.id.pad4);
- Button pad5 = (Button) findViewById(R.id.pad5);
- Button pad6 = (Button) findViewById(R.id.pad6);
- Button pad7 = (Button) findViewById(R.id.pad7);
- Button pad8 = (Button) findViewById(R.id.pad8);
- pad1.setOnClickListener(pad1Click);
- pad2.setOnClickListener(pad1Click);
- pad3.setOnClickListener(pad1Click);
- pad4.setOnClickListener(pad1Click);
- pad5.setOnClickListener(pad1Click);
- pad6.setOnClickListener(pad1Click);
- pad7.setOnClickListener(pad1Click);
- pad8.setOnClickListener(pad1Click);
- }
- public OnClickListener pad1Click = new OnClickListener() {
- public void onClick(View v)
- {
- //try
- switch (v.getId())
- {
- case R.id.pad1:
- padSound = MediaPlayer.create(getApplicationContext(), R.raw.analog_clap);
- padSound.start();
- break;
- case R.id.pad2:
- padSound = MediaPlayer.create(getApplicationContext(), R.raw.analog_kick);
- padSound.start();
- break;
- }
- }
- };
- }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


