javabeaconeddystonekontakt.io

ListAdapter does not work


I have a mainActivity which has a button named monitoring. When I press the button it should show the nearby beacons. When I press the button an error shows up which say's my application stopped working. Here is the code of the button activity:

public class MonitoringBeacons extends AppCompatActivity {

    private ProximityManagerContract proximityManager;

    ListAdapter beaconsAdapter;
    ListView listView;
    List<String> beaconsList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitoring_beacons);
        KontaktSDK.initialize("kasjdhioasjkdoasdjo");

        proximityManager = new ProximityManager(this);
        proximityManager.setEddystoneListener(createEddystoneListener());

        setAdapter();


        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                        String beacon = String.valueOf(adapterView.getItemAtPosition(position));
                        Toast.makeText(MonitoringBeacons.this, beacon, Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }

    protected void setAdapter() {
        if(beaconsList.isEmpty()) {
            Toast.makeText(MonitoringBeacons.this, "No beacons found", Toast.LENGTH_SHORT).show();
            return ;
        }
        beaconsAdapter = new CustomListAdapter(this, beaconsList);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(beaconsAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startScanning();
    }

    @Override
    protected void onStop() {
        proximityManager.stopScanning();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        proximityManager.disconnect();
        proximityManager = null;
        super.onDestroy();
    }

    private void startScanning() {
        proximityManager.connect(new OnServiceReadyListener() {
            @Override
            public void onServiceReady() {
                proximityManager.startScanning();
            }
        });
    }

    private EddystoneListener createEddystoneListener() {
        return new SimpleEddystoneListener() {
            @Override
            public void onEddystoneDiscovered(IEddystoneDevice eddystone, IEddystoneNamespace namespace) {
                beaconsList.add(eddystone.getUniqueId());
                setAdapter();
            }

            @Override
            public void onEddystoneLost(IEddystoneDevice eddystone, IEddystoneNamespace namespace) {
                beaconsList.remove(eddystone.getUniqueId());
                setAdapter();
            }
        };
    }

}

and the code of CustomListAdapter:

public class CustomListAdapter extends BaseAdapter {

    private List<String> beaconsList;
    private Activity activity;
    private LayoutInflater inflater;

    public CustomListAdapter(Activity activity, List<String> beaconsList) {
        this.activity = activity;
        this.beaconsList = beaconsList;
    }

    @Override
    public int getCount() {
        return beaconsList.size();
    }

    @Override
    public Object getItem(int location) {
        return beaconsList.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
            convertView = inflater.inflate(R.layout.custom_row, null);

        String singleBeaconItem = String.valueOf(getItem(position));
        TextView beaconText = (TextView) convertView.findViewById(R.id.BeaconText);
        ImageView beaconImage = (ImageView) convertView.findViewById(R.id.BeaconImage);

        beaconText.setText(singleBeaconItem);
        beaconImage.setImageResource(R.drawable.tough_beacon_1);

        return convertView;
    }
}

Ideally when I press the button the nearby beacons should be appeared as a list, and when a beacon is discovered or lost the list should automaticaly be updated.

EDITED

Stack trace in LogCat

--------- beginning of crash 08-06 17:02:15.276 2559-2559/com.example.panagiotis.beaconsproject E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.panagiotis.beaconsproject, PID: 2559 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.panagiotis.beaconsproject/com.example.panagiotis.beaconsproject.MonitoringBeacons}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference at com.example.panagiotis.beaconsproject.MonitoringBeacons.onCreate(MonitoringBeacons.java:44) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 08-06 17:02:18.087 2559-2559/com.example.panagiotis.beaconsproject I/Process: Sending signal. PID: 2559 SIG: 9 08-06 17:02:22.298 3433-3433/com.example.panagiotis.beaconsproject W/System: ClassLoader referenced unknown path: /data/app/com.example.panagiotis.beaconsproject-1/lib/x86 08-06 17:02:22.455 3433-3433/com.example.panagiotis.beaconsproject W/System: ClassLoader referenced unknown path: /data/app/com.example.panagiotis.beaconsproject-1/lib/x86 08-06 17:02:22.604 3433-3433/com.example.panagiotis.beaconsproject W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 08-06 17:02:22.715 3433-3460/com.example.panagiotis.beaconsproject D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 08-06 17:02:22.718 3433: 3433 D/ ] HostConnection::get() New Host Connection established 0xaa9a71c0, tid 3433
[ 08-06 17:02:22.754 3433: 3460 D/ ] HostConnection::get() New Host Connection established 0xaa9a6920, tid 3460 08-06 17:02:22.768 3433-3460/com.example.panagiotis.beaconsproject I/OpenGLRenderer: Initialized EGL, version 1.4 08-06 17:02:24.070 3433-3460/com.example.panagiotis.beaconsproject E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa9b17f0 08-06 17:02:26.685 3433-3433/com.example.panagiotis.beaconsproject W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=397.5, y[0]=956.6797, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=70853, downTime=67680, deviceId=0, source=0x1002 } 08-06 17:02:26.685 3433-3433/com.example.panagiotis.beaconsproject W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=397.5, y[0]=956.6797, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=70853, downTime=67680, deviceId=0, source=0x1002 } 08-06 17:02:26.686 3433-3433/com.example.panagiotis.beaconsproject W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=397.5, y[0]=956.6797, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=70853, downTime=67680, deviceId=0, source=0x1002 } 08-06 17:02:26.686 3433-3433/com.example.panagiotis.beaconsproject W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=397.5, y[0]=956.6797, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=70853, downTime=67680, deviceId=0, source=0x1002 } 08-06 17:02:28.817 3433-3460/com.example.panagiotis.beaconsproject E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa9b17f0 08-06 17:02:29.373 3433-3460/com.example.panagiotis.beaconsproject E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40938f0 08-06 17:02:29.375 3433-3460/com.example.panagiotis.beaconsproject D/OpenGLRenderer: endAllStagingAnimators on 0xa1d53580 (RippleDrawable) with handle 0xa203f910 08-06 17:02:30.806 3433-3460/com.example.panagiotis.beaconsproject E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4094e60 08-06 17:02:32.811 3433-3460/com.example.panagiotis.beaconsproject E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40938f0 08-06 17:02:35.874 3433-3433/com.example.panagiotis.beaconsproject D/AndroidRuntime: Shutting down VM 08-06 17:02:35.874 3433-3433/com.example.panagiotis.beaconsproject E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.panagiotis.beaconsproject, PID: 3433 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.panagiotis.beaconsproject/com.example.panagiotis.beaconsproject.MonitoringBeacons}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference at com.example.panagiotis.beaconsproject.MonitoringBeacons.onCreate(MonitoringBeacons.java:44) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 08-06 17:02:39.204 3433-3433/com.example.panagiotis.beaconsproject I/Process: Sending signal. PID: 3433 SIG: 9

Any idea?


Solution

  • The stacktrace explains what is happening.

    When this line of code executes:

    listView.setOnItemClickListener(...
    

    The listView has not yet been initialized, so it is null. This causes a NullPointerException.

    That variable is initialized in the setAdapter() method, but it may not be because the method exits early if beaconsList.isEmpty() evaluates to true.

    The simple solution is to move the code that initializes listView to top of that method.