我是Android开发的新手,我试图在列表中显示广播蓝牙信号的设备列表。
MainActivity.java
public class MainActivity extends ListActivity {
private BluetoothManager bManager;
private BluetoothAdapter bAdap;
private listViewAdapter lvAdap;
private Handler mHandler;
private boolean bScanning;
private static long SCAN_PERIOD = 10000;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bManager = (BluetoothManager)getSystemService(BLUETOOTH_SERVICE);
bAdap = bManager.getAdapter();
bScanning = false;
mHandler = new Handler();
tv = (TextView)findViewById(R.id.textView);
lvAdap = new listViewAdapter(this);
ListView lv = (ListView)findViewById(R.id.listView);
lv.setAdapter(lvAdap);
}
@Override
protected void onResume() {
super.onResume();
if(!bAdap.isEnabled())
{
Intent enableBT = new Intent(bAdap.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT,1);
}
scanLeDevice(true);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == 1 && resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
scanLeDevice(false);
lvAdap.clear();
}
/*
@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, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
*/
//List adapter for displaying Bluetooth device name and address
public class listViewAdapter extends BaseAdapter
{
private Context mContext;
private LayoutInflater mLayInflat;
private ArrayList<BluetoothDevice> bDeviceArray = new ArrayList<BluetoothDevice>();
public listViewAdapter(Context context)
{
mContext = context;
mLayInflat = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addDevice(BluetoothDevice device)
{
if(!bDeviceArray.contains(device))
{
bDeviceArray.add(device);
}
}
public BluetoothDevice getDevice(int position)
{
return bDeviceArray.get(position);
}
@Override
public int getCount() {
return bDeviceArray.size();
}
public void clear()
{
bDeviceArray.clear();
}
@Override
public Object getItem(int i)
{
return bDeviceArray.get(i);
}
@Override
public long getItemId(int i)
{
return i;
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {
LinearLayout itemView;
if(convertView == null)
{
itemView = (LinearLayout)mLayInflat.inflate(R.layout.list,parent,false);
}
else
{
itemView = (LinearLayout)convertView;
}
TextView dNameText = (TextView)itemView.findViewById(R.id.device_name);
TextView dAddresstext = (TextView)itemViewfindViewById(R.id.device_address);
BluetoothDevice device = bDeviceArray.get(i);
final String deviceName = device.getName();
if(deviceName != null || deviceName.length()>0)
dNameText.setText(deviceName);
else
dNameText.setText("UNKNOWN DEVICE");
dAddresstext.setText(device.getAddress());
return itemView;
}
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
bScanning = false;
tv.setText("Stopped scanning!");
bAdap.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
tv.setText("Scanning...");
bScanning = true;
bAdap.startLeScan(mLeScanCallback);
} else {
bScanning = false;
bAdap.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
lvAdap.addDevice(device);
lvAdap.notifyDataSetChanged();
}
});
}
};
}
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/device_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"/>
<TextView
android:id="@+id/device_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="14sp"/>
</LinearLayout>
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.newP.inflatetest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Scanning..."
android:id="@+id/textView" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:id="@+id/listView" />
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.newP.inflatetest">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Material.Light.DarkActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我已经包含了所有必要的
进口。由于仿真器不支持蓝牙,我将其部署在手机中。
我不知道究竟是什么错误。每当我尝试启动它时,应用程序都会崩溃。
我通过DDMS收到了此错误消息。
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.newP.inflatetest / com.newP.inflatetest.MainActivity}: java.lang.RuntimeException:您的内容必须具有其id为的ListView 属性是&#39; android.R.id.list&#39;
任何帮助都会很棒。 谢谢。
答案 0 :(得分:0)
您似乎正在使用ListActivity
并且您没有在布局中将ListView
设置为id
,而您尚未定义list
元素使用
尝试将public class MainActivity extends ListActivity
更改为public class MainActivity extends Activity
。
或者,将ListActivity
元素的ID更改为android:id="@+id/list"
,并在您的活动中删除lv
变量的声明,然后只需致电setListAdapter(lvAdap);