Есть метод: discoverDevices(View view)
code:
public class SearchActivity extends Activity {
private BluetoothAdapter bluetoothAdapter;
private BroadcastReceiver discoverDevicesReceiver;
private BroadcastReceiver discoveryFinishedReceiver;
private final List<BluetoothDevice> discoveredDevices = new ArrayList<BluetoothDevice>();
private final List<String> discoveredD = new ArrayList<>();
private ArrayAdapter<BluetoothDevice> listAdapter;
private ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
public void discoverDevices(View view) {
discoveredDevices.clear();
listAdapter.notifyDataSetChanged();
if (discoverDevicesReceiver == null) {
discoverDevicesReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (!discoveredDevices.contains(device)) {
discoveredDevices.add(device);
discoveredD.add(device.getName());
listAdapter.notifyDataSetChanged();
}
}
}
};
}
if (discoveryFinishedReceiver == null) {
discoveryFinishedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (progressDialog != null)
progressDialog.dismiss();
final JSONArray Codes = new JSONArray(discoveredD);
intent = new Intent(SearchActivity.this, SearchActivity2.class);
intent.putExtra("codes", Codes.toString());
startActivity(intent);
unregisterReceiver(discoveryFinishedReceiver);
}
};
}
registerReceiver(discoverDevicesReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
registerReceiver(discoveryFinishedReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
progressDialog = ProgressDialog.show(this, getString(R.string.search_bt), getString(R.string.wait));
bluetoothAdapter.startDiscovery();
}
@Override
public void onPause() {
super.onPause();
bluetoothAdapter.cancelDiscovery();
if (discoverDevicesReceiver != null) {
try {
unregisterReceiver(discoverDevicesReceiver);
} catch (Exception e) {
Log.d("SearchActivity", "Error turn off reciever: " + discoverDevicesReceiver);
}
}
}
}
Мне нужно вызвать метод discoverDevices()
при старте активити (раньше вызывался при нажатии на кнопку). Как вызвать?
log:
09-24 22:56:21.475 12493-12493/com.totalsx.ltmE/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.totalsx.ltm/com.totalsx.ltm.SearchActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) at android.app.ActivityThread.access$700(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5317) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException at com.totalsx.ltm.SearchActivity.discoverDevices(SearchActivity.java:203) at com.totalsx.ltm.SearchActivity.onCreate(SearchActivity.java:195) at android.app.Activity.performCreate(Activity.java:5326) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) at android.app.ActivityThread.access$700(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5317) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)