Android brother is a novice, a recent study service, want to be a simple music player, able to play, pause, (continued), replay, stop.
Met when testing a null pointer error, check for a long time are still do not know what is wrong, you stick the code to find the god pointing.
MainActivity.java:
package ji.shaobin.misicplayer;
import java.io.File;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
public class MainActivity extends Activity {
private EditText editText;
private Button btn_play, btn_pause, btn_restart, btn_stop;
private PlayerService.PlayerBinder binder;
private boolean pause = false;
File file;
Intent service;
private PlayerConnection conn = new PlayerConnection ();
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
new PlayerService ();
init ();
}
public void init () {
editText = (EditText) this.findViewById (R.id.edit);
btn_play = (Button) this.findViewById (R.id.play);
btn_pause = (Button) this.findViewById (R.id.pause);
btn_restart = (Button) this.findViewById (R.id.restart);
btn_stop = (Button) this.findViewById (R.id.stop);
btn_play.setOnClickListener (listener);
btn_pause.setOnClickListener (listener);
btn_restart.setOnClickListener (listener);
btn_stop.setOnClickListener (listener);
service = new Intent (MainActivity.this, PlayerService.class);
String name = editText.getText () toString ();.
service.putExtra ("name", name);
}
OnClickListener listener = new View.OnClickListener () {
@ Override
public void onClick (View v) {
switch (v.getId ()) {
case R.id.play:
bindService (service, conn, BIND_AUTO_CREATE) ;/ / establish service connection
binder.player ();
break;
case R.id.pause:
if (pause) {
binder.pause ();
btn_pause.setText ("Continue");
pause = false;
} Else {
binder.pause ();
btn_pause.setText ("pause");
pause = true;;
}
break;
case R.id.restart:
binder.restart ();
break;
case R.id.stop:
binder.stop ();
}
}
};
@ Override
protected void onDestroy () {
super.onDestroy ();
unbindService (conn);
}
private class PlayerConnection implements ServiceConnection {
@ Override
public void onServiceConnected (ComponentName name, IBinder service) {
binder = (PlayerService.PlayerBinder) service;
}
@ Override
public void onServiceDisconnected (ComponentName name) {
binder = null;
}
}
}
PlayerService.java
package ji.shaobin.misicplayer;
import java.io.File;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;
public class PlayerService extends Service {
private IBinder binder = new PlayerBinder ();
private String path;
private int position;
public MediaPlayer mediaPlayer = new MediaPlayer ();
private File file;
@ Override
public int onStartCommand (Intent intent, int flags, int startId) {
String name = intent.getStringExtra ("name");
file = new File (Environment.getExternalStorageDirectory (), name);
return super.onStartCommand (intent, flags, startId);
}
private void play (int position) {
if (file.exists ()) {
path = file.getAbsolutePath ();
} Else {
path = null;
Toast.makeText (this, "the file does not exist SD card", 1) show ();.
}
try {
mediaPlayer.reset () ;/ / to restore the parameters to the initial state
mediaPlayer.setDataSource (path);
mediaPlayer.prepare () ;/ / buffer
mediaPlayer.setOnPreparedListener (new PreparedListener (position));
} Catch (Exception e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
@ Override
public IBinder onBind (Intent arg0) {
return binder;
}
public class PlayerBinder extends Binder {
public void player () {
if (mediaPlayer! = null) {
play (0);
}
}
public void pause () {
if (mediaPlayer! = null && mediaPlayer.isPlaying ()) {
position = mediaPlayer.getCurrentPosition ();
mediaPlayer.pause ();
} Else {
mediaPlayer.start ();
mediaPlayer.seekTo (position);
}
}
public void restart () {
if (mediaPlayer! = null) {
mediaPlayer.start ();
mediaPlayer.seekTo (0);
}
}
public void stop () {
if (mediaPlayer! = null) {
mediaPlayer.stop ();
mediaPlayer.release ();
}
}
}
private final class PreparedListener implements OnPreparedListener {
private int position;
public PreparedListener (int position) {
this.position = position;
}
@ Override
public void onPrepared (MediaPlayer arg0) {
mediaPlayer.start () ;/ / start playing;
if (position> 0) mediaPlayer.seekTo (position);
}
}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Landlord, you have to print the information posted abnormal ah, or else really can not help you see
Reply:
private PlayerService.PlayerBinder binder;
Looks like there is a problem, you check it.
Reply:
What do you do when a null pointer that can specifically do
Reply:
Thank you. In this error message attached
Reply:
According to your error message, see the error lies in the MainActivity 57 rows onClick () function. Null pointer is likely you which variables are not initialized, set a breakpoint debugging look at this function. Look at that variable is used when there is no assignment, the value is null. Then see if it is forgotten assignment.
Reply:
Hello, the error message has been attached
Reply:
Hello, I also know that there are 57 rows problem ah, but I just do not know where is wrong, and will not break ah debugging with the debug did not see where the problem is
Reply:
That there is a stupid way, you this is a button click event.
Click the button to look at which time, there has been an exception error.
Then look at this case following which the variable is null.
The method, however, by the pilot to be assigned to each variable, if and when an assignment to a variable, the exception will not occur again, it is a question you assign the variable a.
Reply:
Thank you very helpful upstairs, I have already solved the problem. The main problem is: I forgot to call startService (intent) on the play button (play), start the service, resulting binder object is empty. The code will be attached to the modified for everyone to see and learn together.
Reply:
The revised code in the blog, a friend in need, please Forum Search brother blog. Brother blog just opened, seeking to step ~ ~ ~ ~ ~ ~
No comments:
Post a Comment