2020-11-26

Array Adapter is null after initialized (makes no sense!)

my app keeps crashing for a stupid reason! I have looked around and users with similar problems are using fragments, and I am using Activities. My code in the on create method is below.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_select_contact);

    setResult(Activity.RESULT_CANCELED);
    ArrayAdapter<String> mPairedDevicesArrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.device_name);

    ListView mPairedListView = findViewById(R.id.paired_devices);
    mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
    mPairedListView.setOnItemClickListener(mDeviceClickListener);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();

    if (mPairedDevices.size() > 0) {
        /* List of all paired devices */
        findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
        for (BluetoothDevice mDevice : mPairedDevices) {
            mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" + mDevice.getAddress());
        }
    } else {
        /* No paired device */
        String mNoDevices = "None Paired";
   

The arror is as below,

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void

android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at ca.edgarwideman.taxibuddy.ConnectPrinter.onCreate(ConnectPrinter.java:39)

and line number 39 (where the error occurs) is this,

mPairedListView.setAdapter(mPairedDevicesArrayAdapter);


from Recent Questions - Stack Overflow https://ift.tt/2V59gWF
https://ift.tt/eA8V8J

No comments:

Post a Comment