在弹出对话框中显示配对蓝牙设备列表 - setAdapter 空指针异常
Displaying a list of paired bluetooth devices in a popup dialog - setAdapter null pointer exception
我正在尝试创建一个简单的应用程序,当单击操作栏中的 "Connect" 项时,它会在对话框中显示已配对蓝牙设备的列表。这是我正在膨胀的对话框的 XML:
<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();
}
但是当我 运行 在 phone 上执行此操作时,应用程序崩溃并且堆栈跟踪在以下位置显示空指针异常:
myListView.setAdapter(BTArrayAdapter);
当我注释掉上面的行时,"Connect" 按钮会显示一个空白对话框弹出窗口。我认为崩溃与 "BTList" 不在当前视图的上下文中有关,但我不确定如何解决它。
感谢您的帮助。
您必须像这样在 showBTDialog
中从膨胀的布局 ViewLayout
中获取 ListView:
myListView = (ListView) ViewLayout.findViewById(R.id.BTList);
或者这不是你想要做的吗?
我正在尝试创建一个简单的应用程序,当单击操作栏中的 "Connect" 项时,它会在对话框中显示已配对蓝牙设备的列表。这是我正在膨胀的对话框的 XML:
<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();
}
但是当我 运行 在 phone 上执行此操作时,应用程序崩溃并且堆栈跟踪在以下位置显示空指针异常:
myListView.setAdapter(BTArrayAdapter);
当我注释掉上面的行时,"Connect" 按钮会显示一个空白对话框弹出窗口。我认为崩溃与 "BTList" 不在当前视图的上下文中有关,但我不确定如何解决它。
感谢您的帮助。
您必须像这样在 showBTDialog
中从膨胀的布局 ViewLayout
中获取 ListView:
myListView = (ListView) ViewLayout.findViewById(R.id.BTList);
或者这不是你想要做的吗?