2017-10-06 5 views
3

나는 3 액티비티에서 service ~ binding인데, 2에서 제대로 작동하지만 3 번째에서는 오류가 발생했습니다. 여기 바인드 서비스 오류

내 서비스 :

public class ClientBluetoothService extends Service { 
    private Handler handler = new Handler(); 

    private final IBinder mBinder = new LocalBinder(); 

    private ClientBluetooth clientBT; 

    public class LocalBinder extends Binder { 
     ClientBluetoothService getSerivce() { 
      return ClientBluetoothService.this; 
     } 
    } 
    public ClientBluetoothService() { 
     clientBT = new ClientBluetooth(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 

    public void generateToast() { 
     Log.d("HERE", "Service: generateToast()"); 
     Toast.makeText(getApplicationContext(), "WITAJ W SERWISIE", Toast.LENGTH_SHORT).show(); 
    } 

    public void newClient() { 
     clientBT = new ClientBluetooth(); 
    } 

    public void start() { 
     clientBT.start(); 
    } 

    public String getStatus() { 
     return clientBT.getStatus(); 
    } 

    public void disconnect() throws IOException { 
     clientBT.disconnect(); 
    } 
} 

그리고 여기에 오류가있는 내 활동 :

: 나는 오류가있어

public class MeasureActivity extends AppCompatActivity { 

    protected boolean mBound = false; 
    private ClientBluetoothService clientBluetoothService; 

    private TextView textView; 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     Intent intent = new Intent(this, ClientBluetoothService.class); 
     bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
     Log.d("HERE", "Measure: onStart()"); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     if (mBound) { 
      unbindService(mConnection); 
      mBound = false; 
     } 
    } 

    @Override 
    public void setTitle(CharSequence title) { 
     super.setTitle(title); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_measure); 

     setTitle("Measure"); 
     textView = (TextView)findViewById(R.id.textView); 
     clientBluetoothService.generateToast(); 
    } 

    private ServiceConnection mConnection = new ServiceConnection() { 

     @Override 
     public void onServiceConnected(ComponentName className, IBinder service) { 

      ClientBluetoothService.LocalBinder binder = (ClientBluetoothService.LocalBinder) service; 
      clientBluetoothService = binder.getSerivce(); 
      mBound = true; 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName arg0) { 
      mBound = false; 
     } 
    }; 
} 

그리고

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.gmail.krakow.michalt.myecg.ClientBluetoothService.generateToast()' on a null object reference

답변

3

이유를 모르겠다. 다른 2 개의 액티비티와 동작하지만,이 경우 NullPointerException을 얻는 것은 당연합니다. 액티비티 라이프 사이클의 순서는 onState() -> onStart() -> onState() -> onStop() -> onDestroy()와 같이 onCreate() -> onStart() -> onPause() ->

입니다. (https://developer.android.com/guide/components/activities/activity-lifecycle.html)

clientBluetoothService는 onStart에서 수행되는 서비스에 바인딩 된 후에 초기화되지만 onCreate에서 사용되며 onCreate는 항상 NullPointerException을 제공하는 onStart보다 먼저 실행됩니다.

mBound는 clientBluetoothService가 사용 중일 때 초기화되도록하려는 경우 유용 할 수 있습니다.