2015-06-06 45 views
2

我試圖創建一個簡單的應用程序,當在操作欄中的「連接」項目時,在對話框中顯示配對的藍牙設備列表被點擊。下面是我在充氣對話的XML:在彈出對話框中顯示配對的藍牙設備列表 - setAdapter空指針異常

<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"> 

    <ListView 
     android:id="@+id/BTList" 
     android:layout_width="fill_parent" 
     android:layout_height="200dp" > 

    </ListView> 

</LinearLayout> 

下面是MainActivity.java的相關部分:

@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<String>(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(); 

} 

但是,當我的手機,應用程序崩潰和運行這個堆棧跟蹤顯示了一個空指針異常:

myListView.setAdapter(BTArrayAdapter); 

當我註釋掉線之上,「連接」按鈕,將顯示一個空白對話框彈出,因爲它應該。我認爲崩潰與「BTList」不在當前視圖的上下文中有關,但我不知道如何避開它。

感謝您的幫助。

回答

1

你必須從你的虛增佈局ViewLayout讓你的ListView在showBTDialog這樣的:

myListView = (ListView) ViewLayout.findViewById(R.id.BTList); 

,或者不是此你想要做什麼?

+0

我明白了。這現在很有意義。謝謝。 – Shubham