gpt4 book ai didi

android - 一些Android的音板上的ForceClose问题?

转载 作者:行者123 更新时间:2023-12-02 07:18:22 25 4
gpt4 key购买 nike

好的,我的问题是,当我打开应用程序时,当我尝试单击某些歌曲时,它强制关闭。奇怪的是,它在Galaxy Tab上很好用,但不适用于Galaxy S,droid 2,vibrative等。这是我使用的代码:

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();

}

});




}


}

很抱歉,我的代码太长了,我真的需要帮助

编辑:如果有帮助,我总是得到:

java.lang.NullPointerException
MyMain $ 8.onClick()

最佳答案

没有工作的logcat输出或堆栈跟踪,我的猜测是,通过一次将那么多MediaPlayer实例加载到内存中,您正在使大多数设备大呼过瘾!调用MediaPlayer.create()不仅会创建另一个实例,还会在其上调用prepare()并使它准备好滚动。也许尝试更简化的方法:

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();
}

}

此方法将创建 MediaPlayer实例,并仅在需要时播放声音。完成后,侦听器可以释放资源以保持合理的内存占用。

您还可以通过在XML中使用android:onClick来进一步简化此代码,以使所有按钮都调用一个方法,然后基于按钮id调用具有正确ID的 playSound()(当然,除非您的定位目标是1.5)。

希望有帮助!

关于android - 一些Android的音板上的ForceClose问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4961725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com