无法访问我的服务方法

时间:2011-05-22 19:58:45

标签: android

我在我的智慧结束,按照Android devguide上的示例在这里创建绑定服务:http://developer.android.com/guide/topics/fundamentals/bound-services.html

我创建了一个绑定服务来播放媒体文件,但是,每当我尝试从我的活动中访问它的任何公共方法时,我都会在logcat上获得强制关闭错误和NullPointerException。

MediaService.java

public class MediaService extends Service {

private MediaPlayer mediaPlayer;
private SeekBar seeker;
private Boolean curRdy;
private Boolean paused;
private Cursor cursor;
private int columIndex;
private int position;

public class LocalBinder extends Binder {

    MediaService getService() {

        return MediaService.this;

    }

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

      return START_STICKY;

}

@Override

public void onCreate() {

    super.onCreate();

    mediaPlayer = new MediaPlayer();
    curRdy = false;
    paused = false;

}

@Override

public IBinder onBind(Intent intent) {

    return (IBinder) new LocalBinder();

}

PlayerTab.java

public class PlayerTab extends Activity {

private Intent intent;
private SeekBar seeker;
private MediaService serv;

private ServiceConnection servCon = new ServiceConnection() {

    @Override

    public void onServiceConnected(ComponentName className, IBinder service) {

        serv = ((MediaService.LocalBinder) service).getService();

    }

    @Override

    public void onServiceDisconnected(ComponentName arg0) {

        //-----

    }

};

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.player);

    intent = new Intent(PlayerTab.this, MediaService.class);
    seeker = (SeekBar) findViewById(R.id.seeker);

}

@Override

protected void onStart() {

    super.onStart();
    startService(intent);
    bindService(intent, servCon, Context.BIND_AUTO_CREATE);
    serv.loadPlayerSeeker(seeker);

}

serv.loadPlayerSeeker()是MediaService中的公共方法之一,当我尝试访问它或任何其他方法时,它只是失败。该服务将创建并开始正常,它似乎没有正确地与PayerTab活动有关。

1 个答案:

答案 0 :(得分:0)

在调用serv.loadPlayerSeeker(seeker);之后,您无法致电public void onServiceConnected(ComponentName className, IBinder service) {