Forceclose issue on soundboard with some androids?
Ok, my problem is that when I open up the app I made it forces closes when I try to click some of the songs. It is weird bc it works good on Galaxy Tab but not for Galaxy S, droid 2, vibrant, etc. Here is the code I use for the thing:
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MyMain extends Activity {
/** Called when the activity is first created. */
MediaPlayer mpButtonClick;
MediaPlayer pButtonClick;
MediaPlayer oButtonClick;
MediaPlayer tButtonClick;
MediaPlayer sButtonClick;
MediaPlayer aButtonClick;
MediaPlayer dButtonClick;
MediaPlayer cButtonClick;
MediaPlayer lButtonClick;
MediaPlayer himButtonClick;
MediaPlayer herButtonClick;
MediaPlayer hellButtonClick;
MediaPlayer mocButtonClick;
MediaPlayer rasButtonClick;
MediaPlayer raseButtonClick;
MediaPlayer dalekButtonClick;
MediaPlayer sharButtonClick;
MediaPlayer godButtonClick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up the button sounds
mpButtonClick = MediaPlayer.create(this, R.raw.money);
Button bmoney = (Button) findViewById(R.id.money);
bmoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick.start();
}
});
pButtonClick = MediaPlayer.create(this, R.raw.pants);
Button bpants = (Button) findViewById(R.id.pants);
bpants.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pButtonClick.start();
}
});
oButtonClick = MediaPlayer.create(this, R.raw.oath);
Button oath = (Button) findViewById(R.id.oath);
oath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
oButtonClick.start();
}
});
tButtonClick = MediaPlayer.create(this, R.raw.tardis);
Button tardis = (Button) findViewById(R.id.tardis);
tardis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tButtonClick.start();
}
});
sButtonClick = MediaPlayer.create(this, R.raw.screwdriver);
Button screwdriver = (Button) findViewById(R.id.screwdriver);
screwdriver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sButtonClick.start();
}
});
aButtonClick = MediaPlayer.create(this, R.raw.alarm);
Button alarm = (Button) findViewById(R.id.alarm);
alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
aButtonClick.start();
}
});
dButtonClick = MediaPlayer.create(this, R.raw.kame);
Button kame = (Button) findViewById(R.id.kame);
kame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dButtonClick.start();
}
});
cButtonClick = MediaPlayer.create(this, R.raw.card);
Button card = (Button) findViewById(R.id.card);
card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cButtonClick.start();
}
});
lButtonClick = MediaPlayer.create(this, R.raw.laugh);
Button laugh = (Button) findViewById(R.id.laugh);
laugh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
lButtonClick.start();
}
});
himButtonClick = MediaPlayer.create(this, R.raw.him);
Button him = (Button) findViewById(R.id.him);
him.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
himButtonClick.start();
}
});
herButtonClick = MediaPlayer.create(this, R.raw.her);
Button her = (Button) findViewById(R.id.her);
her.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
herButtonClick.start();
}
});
hellButtonClick = MediaPlayer.create(this, R.raw.hell);
Button hell = (Button) findViewById(R.id.hell);
hell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
hellButtonClick.start();
}
});
mocButtonClick = MediaPlayer.create(this, R.raw.mocking);
Button mocking = (Button) findViewById(R.id.mocking);
mocking.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mocButtonClick.start();
}
});
godButtonClick = MediaPlayer.create(this, R.raw.god);
Button god = (Button) findViewById(R.id.god);
god.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
godButtonClick.start();
}
});
rasButtonClick = MediaPlayer.create(this, R.raw.rasengan);
Button rasengan = (Button) findViewById(R.id.rasengan);
rasengan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
rasButtonClick.start();
}
});
raseButtonClick = MediaPlayer.create(this, R.raw.rasen);
Button rasen = (Button) findViewById(R.id.rasen);
rasen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
raseButtonClick.start();
}
});
dalekButtonClick = MediaPlayer.create(this, R.raw.dalek);
Button dalek = (Button) findViewById(R.id.dalek);
dalek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) 开发者_如何转开发{
// TODO Auto-generated method stub
dalekButtonClick.start();
}
});
sharButtonClick = MediaPlayer.create(this, R.raw.sharingan);
Button sharingan = (Button) findViewById(R.id.sharingan);
sharingan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sharButtonClick.start();
}
});
}
}
Sorry for the long code I just really need help
Edit: If it helps I always get:
java.lang.NullPointerException MyMain$8.onClick()
Without haveing a logcat output or stack trace to work from, my guess is that you are making most devices scream for mercy by loading that many MediaPlayer
instances into memory at one time! Calling MediaPlayer.create()
not only creates another instance, it also calls prepare()
on it and get it ready to roll. Perhaps try a more streamlined approach:
public class MyMain extends Activity implements MediaPlayer.OnCompletionListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up the buttons
Button bmoney = (Button) findViewById(R.id.money);
bmoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.money);
}
});
Button bpants = (Button) findViewById(R.id.pants);
bpants.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.pants);
}
});
Button oath = (Button) findViewById(R.id.oath);
oath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.oath);
}
});
Button tardis = (Button) findViewById(R.id.tardis);
tardis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.tardis);
}
});
Button screwdriver = (Button) findViewById(R.id.screwdriver);
screwdriver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.screwdriver);
}
});
Button alarm = (Button) findViewById(R.id.alarm);
alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.alarm);
}
});
Button kame = (Button) findViewById(R.id.kame);
kame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.kame);
}
});
Button card = (Button) findViewById(R.id.card);
card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.card);
}
});
Button laugh = (Button) findViewById(R.id.laugh);
laugh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.laugh);
}
});
Button him = (Button) findViewById(R.id.him);
him.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.him);
}
});
Button her = (Button) findViewById(R.id.her);
her.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.her);
}
});
Button hell = (Button) findViewById(R.id.hell);
hell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.hell);
}
});
Button mocking = (Button) findViewById(R.id.mocking);
mocking.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.mocking);
}
});
Button god = (Button) findViewById(R.id.god);
god.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.god);
}
});
Button rasengan = (Button) findViewById(R.id.rasengan);
rasengan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.rasengan);
}
});
Button rasen = (Button) findViewById(R.id.rasen);
rasen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.rasen);
}
});
Button dalek = (Button) findViewById(R.id.dalek);
dalek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.dalek);
}
});
Button sharingan = (Button) findViewById(R.id.sharingan);
sharingan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.sharingan);
}
});
}
//Consolidate media playing to one location
private void playSound(int resId) {
MediaPlayer mp = MediaPlayer.create(this, resId);
mp.setOnCompletionListener(this);
mp.start();
}
//Completion listener method
public void onCompletion(MediaPlayer mp) {
mp.release();
}
}
This method will create the MediaPlayer
instance and play the sound only at the time you need it. Once complete, the listener can release the resources to keep your memory footprint reasonable.
You could also streamline this code further by making use of android:onClick in your XML to have all the buttons call one method, and then call playSound()
with the right id based on the button id (unless your targeting 1.5, of course).
Hope that helps!
精彩评论