title: Displaying a list of paired bluetooth devices in a popup dialog - setAdapter null pointer exception
body: <p>I'm trying to create a simple app that displays a list of paired bluetooth devices in a dialog box when a "Connect" item in the action bar is clicked. Here is the XML of the dialog I'm inflating:</p>
  
  <pre><code>&lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/bt_list"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;ListView
          android:id="@+id/BTList"
          android:layout_width="fill_parent"
          android:layout_height="200dp" &gt;
  
      &lt;/ListView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here is the relevant portion of MainActivity.java:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
      TextView statusText = (TextView) findViewById(R.id.textDeviceID);
  
      myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  
      if (myBluetoothAdapter == null) {
          statusText.setText("Bluetooth not supported on this phone");
      }
      else {
          statusText.setText("Waiting to connect");
      }
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  
      super.onOptionsItemSelected(item);
  
      if(item.getItemId()==R.id.connect) {
          if (!myBluetoothAdapter.isEnabled()) {
              Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
              startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);
          }
  
          showBTDialog();
      }
  
      else if(item.getItemId()==R.id.action_settings) {
          // TODO
      }
  
      return true;
  }
  
  public void showBTDialog() {
  
      final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
      final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
      final View Viewlayout = inflater.inflate(R.layout.bt_list, (ViewGroup) findViewById(R.id.bt_list));
  
      popDialog.setTitle("Paired Bluetooth Devices");
      popDialog.setView(Viewlayout);
  
      // create the arrayAdapter that contains the BTDevices, and set it to a ListView
      myListView = (ListView) findViewById(R.id.BTList);
      BTArrayAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1);
      myListView.setAdapter(BTArrayAdapter);
  
      // get paired devices
      pairedDevices = myBluetoothAdapter.getBondedDevices();
  
      // put it's one to the adapter
      for(BluetoothDevice device : pairedDevices)
          BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());
  
      // Button OK
      popDialog.setPositiveButton("Pair",
              new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                      dialog.dismiss();
                  }
  
              });
  
      // Create popup and show
      popDialog.create();
      popDialog.show();
  
  }
  </code></pre>
  
  <p>But when I run this on the phone, the app crashes and the stack trace shows a null pointer exception at:</p>
  
  <pre><code>myListView.setAdapter(BTArrayAdapter);
  </code></pre>
  
  <p>When I comment out the line above, the "Connect" button displays a blank dialog popup as it should. I think the crash something to do with "BTList" not being in the context of the current view, but I'm not sure how to get around it. </p>
  
  <p>Thanks for your help.</p>