2014-06-05 1 views
1

여기가 내 코드입니다.서비스가 활동에서 바인드되지 않았습니다. 널 포인터 예외

public class ScheduleClient { 

    // The hook into our service 
    private ScheduleService mBoundService; 
    // The context to start the service in 
    private Context mContext; 
    // A flag if we are connected to the service or not 
    private boolean mIsBound; 

    public ScheduleClient(Context context) { 
     mContext = context; 
    } 


    public void doBindService() { 
     // Establish a connection with our service 
     mContext.bindService(new Intent(mContext, ScheduleService.class), 
       mConnection, Context.BIND_AUTO_CREATE); 
     mIsBound = true; 
    } 


    private ServiceConnection mConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName className, IBinder service) { 
      // This is called when the connection with our service has been 
      // established, 
      // giving us the service object we can use to interact with our 
      // service. 
      mBoundService = ((ScheduleService.ServiceBinder) service) 
        .getService(); 
     } 

     public void onServiceDisconnected(ComponentName className) { 
      mBoundService = null; 
     } 
    }; 

    /** 
    * Tell our service to set an alarm for the given date 
    * 
    * @param c 
    *   a date to set the notification for 
    */ 
    public void setAlarmForNotification(Calendar c) { 
     if (mBoundService != null) 
      mBoundService.setAlarm(c); 
     else 
      Log.e("@ScheduleClient", "mBoundService is null"); 
    } 

    public void cancelAlarmForNotification() { 
     mBoundService.cancelAlarm(); 
    } 

    /** 
    * When you have finished with the service call this method to stop it 
    * releasing your connection and resources 
    */ 
    public void doUnbindService() { 
     if (mIsBound) { 
      // Detach our existing connection. 
      mContext.unbindService(mConnection); 
      mIsBound = false; 
     } 
    } 
} 

매니페스트 파일

<service android:name="com.mobtecnica.inreez.reminder.ScheduleService" > 
    </service> 

여기 mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);는 항상 false를 반환합니다. 그래서 mBoundService이 null이되었습니다. 이는 서비스가 묶여 있지 않다는 것을 의미합니다. 이 문제를 어떻게 해결할 수 있습니까? 신청서에 알림을 설정하려고합니다. 어떤 도움을 주시면 감사하겠습니다.

답변

0

글쎄, 서비스를 제한하고 따라서 mBoundService을 초기화하는 방법은 doBindService()입니다. 이것은 결코 호출되지 않습니다. 보세요 here, 당신이 필요로하는 모든 것을하는 아주 좋은 예가 있습니다.

+0

예 동일한 것을 호출했습니다. 'scheduleClient = new ScheduleClient (DashBoard.this); \t \t scheduleClient.doBindService();' –