Java setAdapter(ArrayAdapter)正在使应用程序崩溃
我正在尝试使用配对的蓝牙设备填充列表视图。我在MainActivity中尝试使用ListView来实现这一点,但效果非常好。然而,当我在另一个活动中尝试使用ListView时,应用程序崩溃了。我基本上希望在弹出对话框中填充ListView 代码如下: activity\u device\u list.xml(MainActivity)Java setAdapter(ArrayAdapter)正在使应用程序崩溃,java,android,listview,bluetooth,Java,Android,Listview,Bluetooth,我正在尝试使用配对的蓝牙设备填充列表视图。我在MainActivity中尝试使用ListView来实现这一点,但效果非常好。然而,当我在另一个活动中尝试使用ListView时,应用程序崩溃了。我基本上希望在弹出对话框中填充ListView 代码如下: activity\u device\u list.xml(MainActivity) device_dialog.xml DeviceList.java package example.btmodule; 导入android.bluetoo
device_dialog.xml
DeviceList.java
package example.btmodule;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v7.app.AlertDialog;
导入android.support.v7.app.AppActivity;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入java.util.ArrayList;
导入java.util.Set;
导入静态示例.btmodule.R.layout.activity\u设备\u列表;
公共类DeviceList扩展了AppCompative活动{
列表视图设备主义者;
专用蓝牙适配器myBluetooth=null;
专用配对设备;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(活动\设备\列表);
myBluetooth=BluetoothAdapter.getDefaultAdapter();
如果(myBluetooth==null)
{
//显示一个男装。该设备没有蓝牙适配器
Toast.makeText(getApplicationContext(),R.string.bluetooth_不可用,Toast.LENGTH_LONG.show();
//完成apk
完成();
}
否则{
如果(myBluetooth.isEnabled()){
}否则{
//要求用户打开蓝牙
Intent-turnBTon=新的Intent(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(turnBTon,1);
}
}
}
私有无效PairedDeviceList()
{
pairedDevices=myBluetooth.getBondedDevices();
ArrayList=新建ArrayList();
devicelist=(ListView)findViewById(R.id.ListDeviceDialog);
如果(pairedDevices.size()>0)
{
用于(蓝牙设备bt:pairedDevices)
{
list.add(bt.getName()+“\n”+bt.getAddress());//获取设备的名称和地址
}
}
其他的
{
Toast.makeText(getApplicationContext(),R.string.no_devices_found,Toast.LENGTH_LONG).show();
}
最终ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,list);
devicelist.setAdapter(适配器);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
//菜单项选择
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理项目选择
开关(item.getItemId()){
案例R.id.action\u connect:
showDialog();
PairedDeviceList();
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
//显示已连接设备的对话框
私有void showDialog(){
AlertDialog.Builder mBuilder=新建AlertDialog.Builder(DeviceList.this);
View mView=GetLayoutFlater()。充气(R.layout.device_对话框,空);
mBuilder.setView(mView);
AlertDialog=mBuilder.create();
dialog.show();
}
}
如果我将
devicelist
更改为R.id.listDevicesMain
,则代码工作正常。您调用了错误的列表视图,列表视图的id如下所示
<ListView
android:id="@+id/listDevicesMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
您正在尝试调用id为的listview
devicelist=(ListView)findViewById(R.id.ListDeviceDialog)代码>
希望这能解决问题。设备列表=(ListView)findViewById(R.id.ListDeviceDialog)代码>-这里您正在活动布局中查找列表设备对话框
,因此设备列表
变为空。您应该添加devicelist=(ListView)mView.findViewById(R.id.ListDeviceDialog)代码>到showDialog()方法,并将与搜索配对设备和设置适配器相关的其余操作带到其中
您还可以从showDialog调用pairedDevicesList,并传递listDevicesDialog所在的视图:
private void pairedDevicesList(View dialogView)
{
pairedDevices = myBluetooth.getBondedDevices();
ArrayList list = new ArrayList();
devicelist = (ListView) dialogView.findViewById(R.id.listDevicesDialog);
...
}
// show the dialog for connected devices
private void showDialog()
{
AlertDialog.Builder mBuilder = new AlertDialog.Builder(DeviceList.this);
View mView = getLayoutInflater().inflate(R.layout.device_dialog, null);
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
pairedDevicesList(mView);
}
你能添加日志吗?我在device_dialog.xml中有另一个名为listDevicesDialog
的列表视图,这就是我试图调用的。让我告诉你,你的列表视图必须在活动布局中,但布局中唯一的列表视图是这个,因此如果你想使用该列表视图,只需更改你在活动中调用的布局是的,我根本没有注意到。。那是我的错。无论如何谢谢你!你太棒了。我知道我没有在正确的位置查找对话框listview有问题。。非常感谢!
package example.btmodule;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
import static example.btmodule.R.layout.activity_device_list;
public class DeviceList extends AppCompatActivity {
ListView devicelist;
private BluetoothAdapter myBluetooth = null;
private Set<BluetoothDevice> pairedDevices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_device_list);
myBluetooth = BluetoothAdapter.getDefaultAdapter();
if(myBluetooth == null)
{
//Show a mensag. that thedevice has no bluetooth adapter
Toast.makeText(getApplicationContext(), R.string.bluetooth_unavailable, Toast.LENGTH_LONG).show();
//finish apk
finish();
}
else {
if (myBluetooth.isEnabled()) {
} else {
//Ask to the user turn the bluetooth on
Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnBTon, 1);
}
}
}
private void pairedDevicesList()
{
pairedDevices = myBluetooth.getBondedDevices();
ArrayList list = new ArrayList();
devicelist = (ListView)findViewById(R.id.listDevicesDialog);
if (pairedDevices.size()>0)
{
for(BluetoothDevice bt : pairedDevices)
{
list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address
}
}
else
{
Toast.makeText(getApplicationContext(), R.string.no_devices_found, Toast.LENGTH_LONG).show();
}
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
devicelist.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
// menu item selection
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_connect:
showDialog();
pairedDevicesList();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// show the dialog for connected devices
private void showDialog(){
AlertDialog.Builder mBuilder = new AlertDialog.Builder(DeviceList.this);
View mView = getLayoutInflater().inflate(R.layout.device_dialog, null);
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
}
<ListView
android:id="@+id/listDevicesMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
private void pairedDevicesList(View dialogView)
{
pairedDevices = myBluetooth.getBondedDevices();
ArrayList list = new ArrayList();
devicelist = (ListView) dialogView.findViewById(R.id.listDevicesDialog);
...
}
// show the dialog for connected devices
private void showDialog()
{
AlertDialog.Builder mBuilder = new AlertDialog.Builder(DeviceList.this);
View mView = getLayoutInflater().inflate(R.layout.device_dialog, null);
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
pairedDevicesList(mView);
}