App stopping without any Errors

Hi,

First of all thank you for the airport example, it helped. I have a question regarding ListView, I don’t get any errors but whenever I run the app it gives me an error saying “The app has stopped”.
Following is the Listview section I am trying to achieve.

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewObj = (TextView) findViewById(txt1);
        lst1= (ListView)findViewById(R.id.lst1);
        beaconManager = new BeaconManager(this);
        beaconManager.setRangingListener(new BeaconManager.RangingListener() {
            @Override
            public void onBeaconsDiscovered(Region region, List<Beacon> list) {
                if (!list.isEmpty()) {
                    Beacon nearestBeacon = list.get(0);
                    List<String> places = placesNearBeacon(nearestBeacon);
                    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getBaseContext(), R.layout.activity_main, places);
                    lst1.setAdapter(adapter1);
                   
                  //  Log.d("Airport", "Nearest places: " + places);
                }
            }
        });
        region = new Region("ranged region", UUID.fromString("XXXXX-XXXX-XXXXXX-XXXXXXXXXXX"), null, null);
    }

Any crash stack traces in the ADB or Android Studio console? It’s very hard to say why your app is crashing otherwise.

I tried Debugging it. It works when I remove the ListView section but I want it to display the items in ListView. Which is as follow:

ArrayAdapter adapter1 = new ArrayAdapter(getBaseContext(), R.layout.activity_main, places);
lst1.setAdapter(adapter1);

I am using a ListView widget to display the contents of places (Airport example).

I think you might be using the ArrayAdapter wrong. It’s supposed to take the layout of the individual row I think, and you seem to be passing the entire activity to it.

Here’s how I went about implementing the ListView in the Airport tutorial some time ago, and this worked for me:

I just did a quick exercise of implementing the ListView myself, and here’s what I ended up with:

  1. I added the ListView with ID “listView” to the layout.

  2. In the MainActivity, I added these lines in onCreate:

    ListView listView = (ListView) findViewById(R.id.listView);
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
    listView.setAdapter(adapter);
    

    This just gets the list view and sets it up with a data source (i.e., “Adapter” in Android nomenclature) that is a simple Array of Strings. It uses a built-in “android.R.layout.simple_list_item_1” layout for the list rows.

  3. Just below the “TODO: update the UI here,” I added:

    adapter.clear();
    adapter.addAll(places);
    

    This replaces the data that backs the ListView with the list of places obtained from the placesNearBeacon method.