Setting adapter on ListView

I was writing some code to connect my app to an iBeacon following the examples for Android, but my code continue to crash when i copy and paste these lines:

adapter = new LeDeviceListAdapter(this);
ListView list = (ListView) findViewById(R.id.device_list);
list.setAdapter(adapter);
list.setOnItemClickListener(createOnItemClickListener());

expecially on "list.setAdapter(adapter);" and generates NullPointerException in the "adapter".

Originally I was using the class LeDeviceListAdapter without the Inflater, but I changed my mind and I went back to the original implementation.

It is because your inflated view does not have ListView with id device_list. It needs to be present when you are using findViewByID.

But I have a ListView with id device_list.
It's the same ListView used in the example (main.xml).

If this line "list.setAdapter(adapter);" generates NullPointerException and adapter is constructed above, that indicates that list variable is null.

Can you paste your xml and activity?

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/device_list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

and the activity is the same in question's text:
public class BluetoothActivity extends Activity {
some global var
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);

    // Configure verbose debug logging.
    L.enableDebugLogging(true);

    // Configure device list.
    adapter = new LeDeviceListAdapter(this);
    ListView list = (ListView) findViewById(R.id.device_list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(createOnItemClickListener());
    // Configure BeaconManager.
    beaconManager = new BeaconManager(this);
    beaconManager.setRangingListener(new BeaconManager.RangingListener() {
        @Override
        public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
            // Note that results are not delivered on UI thread.
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // Note that beacons reported here are already sorted by estimated
                    // distance between device and beacon.
                    adapter.replaceWith(beacons);
                }
            });
        }
    });

some other code

Is XML files names activity_bluetooth.xml? Maybe you inflate wrong layout and ListView cannot be found.

Nope...
It's "menu.xml". Maybe in process of copy&paste I forgot to put that ListView in my layout...

Good, you solved your problem :).

Don't speak too early :)
I'm trying to apply the solution...

Now I'm scanning but I can't find any beacon in range, even if my beacon is really near to the phone

Does Estimote app from Play Store find the beacon?

Yes, and also the Android Demo.
Maybe I'm missing some code?

In onCreate i have
// Configure BeaconManager.
beaconManager = new BeaconManager(this);
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
// Note that results are not delivered on UI thread.
runOnUiThread(new Runnable() {
@Override
public void run() {
// Note that beacons reported here are already sorted by estimated
// distance between device and beacon.
adapter.replaceWith(beacons);
}
});
}
});

and in my method I have
private void connectToService() {
TextView iBeacon = (TextView) findViewById(R.id.beaconConnectionText);
iBeacon.setText(CONNECTINGTOBEACON);

    adapter.replaceWith(Collections.<Beacon>emptyList());
    beaconManager.connect(new BeaconManager.ServiceReadyCallback(){

        @Override
        public void onServiceReady() {
            try {
                beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
            } catch (RemoteException e) {
                Toast.makeText(BluetoothActivity.this, "Cannot start ranging, something terrible happened",
                        Toast.LENGTH_LONG).show();
                Log.e(TAG, "Cannot start ranging", e);
            }
        }
    });

}

private AdapterView.OnItemClickListener createOnItemClickListener() {
    return new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY) != null) {
                try {
                    Class<?> clazz = Class.forName(getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY));
                    Intent intent = new Intent(BluetoothActivity.this, clazz);
                    intent.putExtra(EXTRAS_BEACON, adapter.getItem(position));
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    Log.e(TAG, "Finding class by name failed", e);
                }
            }
        }
    };
}

Hard to tell.

1) Can you put whole activity on gist.github.com?
2) Can you print found beacons when onBeaconsDiscovered is invoked?

This is the activity:
https://gist.githubusercontent.com/Shadie6/7d5b043368ad288e4173/raw/9a2ed8003eb5010561f637d8645f1a49540cd499/gistfile1.txt

For point 2:
"iBeacon in range: 1"
It seems that I find the beacon but I can't get any data from it

So your activity found actually 1 beacon that you are interested in.

Problem is probably in adapter class since it is responsible for showing beacons in the list. Have you modified it?

Is it possible to add an animation to the new listview ?I want to do this but confused.Please help.