我是新來的Android開發,我試圖顯示列表中廣播藍牙信號的設備列表。這個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:你的內容必須有一個ListView的ID 屬性是'android.R.id.list'
任何幫助將是偉大的。 謝謝。
如果你開始從一個IDE中的應用程序,你可以查看原因在'LogCat'中崩潰。沒有發佈堆棧跟蹤,我不確定你會得到多少幫助。 – Titus
@Titus我已更新錯誤消息的問題。 – Illuminati0x5B