Yous
Yous

Reputation: 3

ListView.setAdapter(ArrayAdapter) is crashing app

I am trying to populate a listView with paired bluetooth devices. I tried doing so with a ListView in my MainActivity and it worked perfectly. However, when I tried it with a ListView in a different activity it crashed the app. I basically want to populate a ListView in a pop-up dialog box.

Here is the code:

activity_device_list.xml (MainActivity)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <ListView
        android:id="@+id/listDevicesMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

device_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.12"
    android:gravity="center"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:text="Paired Devices"
    android:textSize="25sp" />

<ListView
    android:id="@+id/listDevicesDialog"
    android:layout_width="match_parent"
    android:layout_height="395dp"
    android:layout_weight="0.38" />
</LinearLayout>

DeviceList.java

package example.btmodule;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

import static example.btmodule.R.layout.activity_device_list;

public class DeviceList extends AppCompatActivity {

    ListView devicelist;

    private BluetoothAdapter myBluetooth = null;
    private Set<BluetoothDevice> pairedDevices;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_device_list);

        myBluetooth = BluetoothAdapter.getDefaultAdapter();

        if(myBluetooth == null)
        {
            //Show a mensag. that thedevice has no bluetooth adapter
            Toast.makeText(getApplicationContext(), R.string.bluetooth_unavailable, Toast.LENGTH_LONG).show();
            //finish apk
            finish();
        }
        else {
            if (myBluetooth.isEnabled()) {
            } else {
                //Ask to the user turn the bluetooth on
                Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(turnBTon, 1);
            }
        }
    }

    private void pairedDevicesList()
    {
        pairedDevices = myBluetooth.getBondedDevices();
        ArrayList list = new ArrayList();

        devicelist = (ListView)findViewById(R.id.listDevicesDialog);

        if (pairedDevices.size()>0)
        {
            for(BluetoothDevice bt : pairedDevices)
            {
                list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address
            }
        }
        else
        {
            Toast.makeText(getApplicationContext(), R.string.no_devices_found, Toast.LENGTH_LONG).show();
        }

        final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
        devicelist.setAdapter(adapter);

    }

    @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_main, menu);
        return true;
    }

    // menu item selection
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_connect:
                showDialog();
                pairedDevicesList();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    // show the dialog for connected devices
    private void showDialog(){
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(DeviceList.this);
        View mView = getLayoutInflater().inflate(R.layout.device_dialog, null);
        mBuilder.setView(mView);
        AlertDialog dialog = mBuilder.create();
        dialog.show();
    }

}

If I change devicelist to R.id.listDevicesMain the code works perfectly fine.

Upvotes: 0

Views: 972

Answers (2)

Karol Jurski
Karol Jurski

Reputation: 180

devicelist = (ListView)findViewById(R.id.listDevicesDialog); - here you are looking for listDevicesDialog in activity layout, so devicesList becomes null. You should add devicelist = (ListView) mView.findViewById(R.id.listDevicesDialog); to showDialog() method and bring there rest of the operations related with searching for paired devices and setting adapter.

You can also call pairedDevicesList from showDialog and pass view where listDevicesDialog is:

private void pairedDevicesList(View dialogView)
{
    pairedDevices = myBluetooth.getBondedDevices();
    ArrayList list = new ArrayList();
    devicelist = (ListView) dialogView.findViewById(R.id.listDevicesDialog); 
...
}
// show the dialog for connected devices
private void showDialog()
{
    AlertDialog.Builder mBuilder = new AlertDialog.Builder(DeviceList.this);
    View mView = getLayoutInflater().inflate(R.layout.device_dialog, null);
    mBuilder.setView(mView);
    AlertDialog dialog = mBuilder.create();
    dialog.show();
    pairedDevicesList(mView);
}

Upvotes: 0

SAVVY
SAVVY

Reputation: 850

you calling the wrong list view the id of the list view as you have given is

 <ListView
    android:id="@+id/listDevicesMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

and you are trying to call the listview with id devicelist = (ListView)findViewById(R.id.listDevicesDialog); hope this will solve the issue.

Upvotes: 1

Related Questions