我的 Java 代码在这里,错误显示在一行中: ListView newDevicesListView = (ListView) findViewById(R.id.new_devices)
new_devices 添加了 activity_main.xml
我是android应用程序开发的新手,请帮忙。
MainActivity.java
package com.example.sample;
All imports added here
public class MainActivity extends Activity implements View.OnClickListener{
Button btn;
BluetoothDevice BTdevice;
BluetoothAdapter bluetoothAdapter;
ArrayAdapter<String> btArrayAdapter; /** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn = new Button(this);
btn.setOnClickListener(this);
//setContentView(R.layout.activity_main);
setContentView(btn);
}
public void onClick(View view)
{
//updateTime();
final int REQUEST_ENABLE_BT = 1;
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter != null)
{
//btArrayAdapter.clear();
Log.d("ARRAY ADAPTER"," I AM INITIALIZED ");
if (bluetoothAdapter.isDiscovering())
{
bluetoothAdapter.cancelDiscovery();
}
if (!bluetoothAdapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
updateTime();
setContentView(R.layout.activity_main);
bluetoothAdapter.startDiscovery();
Log.d("Discovery","Completed Discovery");
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
Log.d("Intent Filter","After Intent filter call");
registerReceiver(ActionFoundReceiver, filter);
Log.d("Intent Filter","After Register recieve call");
}
else
{
AlertDialog alertdialog = new AlertDialog.Builder(this).create();
alertdialog.setTitle("Bluetooth Adapter");
alertdialog.setMessage("Bluetooth not supported in the device");
}
ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
btArrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_main);
newDevicesListView.setAdapter(btArrayAdapter);
//btDevList.setAdapter(btArrayAdapter);
// Register the BroadcastReceiver
//registerReceiver(ActionFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver()
{
@Override public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
Log.d("Broadcastreciever","Inside Broadcast reciever");
String action = intent.getAction();
Log.d("Broadcastreciever","After Getaction");
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
Log.d("Broadcastreciever","Inside Action found");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d("Broadcastreciever","After get Parcelable");
btArrayAdapter.add(device.getName() + "\n" + device.getAddress()); //Found Crash here
Log.d("Broadcastreciever","After get Device address");
btArrayAdapter.notifyDataSetChanged();
Log.d("Broadcastreciever","After notify data changed");
}
}
};
Activity_Main.xml looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<ListView
//android:id="@+id/new_devices"
android:id="@+id/android:new_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
</ListView>
</RelativeLayout>