2017-10-06 2 views
3

3アクティビティでbindingからserviceになっています.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が発生するのは当然です。アクティビティライフサイクルの順序は次のとおりです。

onCreate() - > onStart() - > onResume() - > onPause() - > onStop() - > onDestroy()です。 (https://developer.android.com/guide/components/activities/activity-lifecycle.html

clientBluetoothServiceはonStartで行われたサービスにバインドされた後に初期化されますが、onCreateで使用され、onCreateは常にonStartより前に実行され、NullPointerExceptionを返します。

mBoundは、clientBluetoothServiceが使用中に初期化されていることを確認したい場合に便利です。