已更新
我有一个音乐播放器应用程序,最近,我试图实现一个SeekBar。但是在启动应用程序时,我遇到了运行时异常为什么?
我的代码:
super.onCreate(SavedInstanceState);
setContentView(R.layout.media_player_layout);
Intent intent = getIntent();
String track = intent.getDataString();
TextView txt = (TextView)findViewById(R.id.txt);
txt.setGravity(Gravity.CENTER);
txt.setText(track);
ImageView img = (ImageView)findViewById(R.id.img);
img.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
final Uri uri = getIntent() != null ? getIntent().getData() : null;
final SharedPreferences premiumSettings = getSharedPreferences("PREMIUM", Context.MODE_PRIVATE);
final boolean isPremium = premiumSettings.getBoolean("isPremium", false);
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(this);
if (isPremium) {
musicPlayer = MediaPlayer.create(MusicPlayer.this, uri);
seekBar.setMax(musicPlayer.getDuration());
try {
musicPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
musicPlayer.start();
final ImageButton play = (ImageButton) findViewById(R.id.play);
if(!musicPlayer.isPlaying()){
play.setImageResource(android.R.drawable.ic_media_play);
}
else{
play.setImageResource(android.R.drawable.ic_media_pause);
}
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = musicPlayer.getCurrentPosition();
if(musicPlayer.isPlaying()){
musicPlayer.pause();
play.setImageResource(android.R.drawable.ic_media_play);
}
else {
musicPlayer.seekTo(position);
musicPlayer.start();
play.setImageResource(android.R.drawable.ic_media_pause);
}}
});
ImageButton exit = (ImageButton) findViewById(R.id.exit);
exit.setImageResource(android.R.drawable.btn_dialog);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (musicPlayer.isPlaying()) {
musicPlayer.stop();
}
musicPlayer.release();
musicPlayer = null;
finish();
}
});
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(MusicPlayer.this);
builder.setTitle("Error");
builder.setMessage("You are not premium user. Please enter the promocode or buy the full version");
builder.setIcon(R.drawable.holo_dark_action_info);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}}
public void onBackPressed () {
if(musicPlayer.isPlaying()) {
musicPlayer.stop();
}
musicPlayer = null;
finish();
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(b){
musicPlayer.seekTo(i);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
和我的logcat:
Process: com.tproductions.Openit, PID: 13440
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tproductions.Openit/com.tproductions.Openit.MusicPlayer}: java.lang.IllegalStateException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer.prepare(Native Method)
at com.tproductions.Openit.MusicPlayer.onCreate(MusicPlayer.java:46)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
非常感谢您的回答。我在互联网上看了一眼,但找不到合适的答案。所以请不要低估这个问题。非常感谢你的时间
答案 0 :(得分:0)
看起来好像是在实例化之前访问musicPlayer
成员
seekBar.setMax(musicPlayer.getDuration());
if (isPremium) {
musicPlayer = MediaPlayer.create(MusicPlayer.this, uri);
应该是
if (isPremium) {
musicPlayer = MediaPlayer.create(MusicPlayer.this, uri);
seekBar.setMax(musicPlayer.getDuration());