我的 ListView 代码有点问题,它应该显示尚未与设备配对的可用设备列表。
我已包含以下错误:
FATAL EXCEPTION: main
at com.zephyr.bolt.UnpairedDeviceViewer$StableArrayAdapter.getItemId(UnpairedDeviceViewer.java:110)
at android.widget.AbsListView.obtainView(AbsListView.java:2198)
(遗憾的是,我没有包括整个错误,错误在我的平板电脑上,我似乎无法从 AIDE 中复制出来)
下面是引用的类文件,它是 XML 文件
UnpairedDeviceViewer.java
package com.zephyr.bolt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class UnpairedDeviceViewer extends Activity{
Activity a = this;
private ListView listview;
private Button b1;
public StableArrayAdapter adapter;
private ArrayList strings;
private BroadcastReceiver recv;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.udv);
b1 = (Button) findViewById(R.id.b5);
listview = (ListView) findViewById(R.id.l2);
strings = new ArrayList<String>();
adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, strings);
recv = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
String action = arg1.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
adapter.add(device.getName() + "\n" + device.getAddress());
adapter.notifyDataSetChanged();
}
else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
{
Toast.makeText(a, "Device Discovery Started", Toast.LENGTH_LONG).show();
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
Toast.makeText(a, "Device Discovery Completed", Toast.LENGTH_LONG).show();
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(recv, filter);
listview.setAdapter(adapter);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.bTAdapter.startDiscovery();
}
});
}
protected void onDestroy()
{
MainActivity.bTAdapter.cancelDiscovery();
unregisterReceiver(recv);
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
udv.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:id = "@+id/b5"
android:layout_centerHorizontal="true"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "Begin Discovery"
/>
<ListView
android:id = "@+id/l2"
android:layout_below="@+id/b5"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>