title: How to request Android user to enable Bluetooth through a click?
body: <p>from <a href="http://developer.android.com/guide/topics/connectivity/bluetooth.html" rel="noreferrer">http://developer.android.com/guide/topics/connectivity/bluetooth.html</a> i know that i need to the following to request the user to enable his BT:</p>
  
  <pre><code>if (!mBluetoothAdapter.isEnabled()) 
  {
  Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  }
  </code></pre>
  
  <p>but the question is how to use it in a class? why does my code crash whenever click on the button of this activity:</p>
  
  <pre><code>public class Opponents extends Activity 
  {
        private final static int REQUEST_ENABLE_BT=1;
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  
        @Override
        protected void onCreate(Bundle savedInstancesState)
        {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.opponents);
  
          final Button button1 = (Button) findViewById(R.id.button1);
          button1.setOnClickListener(new View.OnClickListener() 
          {
            public void onClick(View view)
            {
              if(!mBluetoothAdapter.isEnabled())
              {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
              }
            }
          });
  }
  </code></pre>