由于setadapter功能的原因,应用程序不断崩溃 我的最终目标是拥有一个连接到蓝牙的程序,并显示一个可以点击配对的蓝牙设备的列表视图。我经历了很多教程和论坛,无法让它发挥作用。任何正确方向的帮助将不胜感激。谢谢 //我的主要代码是:
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ListFragment;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
public static int Request_Bluetooth = 1;
int bt = 1;
public ArrayList<BluetoothDevice> btDevices = new ArrayList<>();
ListView newDevices;
TextView tvDeviceName;
BluetoothAdapter Bluetooth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
Bluetooth = BluetoothAdapter.getDefaultAdapter();
newDevices = (ListView) findViewById(R.id.newDevices);
tvDeviceName = (TextView) findViewById(R.id.tvDeviceName);
btDevices = new ArrayList<>();
if(Bluetooth == null){
new AlertDialog.Builder(this)
.setTitle("Bluetooth")
.setMessage("This device does not support Bluetooth")
.setPositiveButton("Main Menu",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
if (!Bluetooth.isEnabled()){
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enable, Request_Bluetooth);
}
Intent discoverable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,1000);
startActivity(discoverable);
Set<BluetoothDevice> pairedDevices = Bluetooth.getBondedDevices();
ArrayAdapter<String> s = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, R.id.newDevices);
for(BluetoothDevice bt : pairedDevices) {
s.add(bt.getName());
//Used to check that the devices are being captured
// tvDeviceName.append("\n Device:" + bt.getName() + ", " +bt);
}
newDevices.setAdapter(s);
}
}
请帮忙