Android Listview – Only the original thread that created a view hierarchy can touch its views

As per subject, I got this error message when it try to populate the data in ListView.

06-10 15:22:14.184    9781-9905/xxxxx E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[BeaconIntentProcessor]
    Process: xxxxx, PID: 9781
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
            at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)
            at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2944)
            at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
            at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
            at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
            at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
            at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
            at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:685)
            at android.view.View.setFlags(View.java:9691)
            at android.view.View.setFocusableInTouchMode(View.java:6771)
            at android.widget.AdapterView.checkFocus(AdapterView.java:727)
            at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:822)
            at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6140)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
            at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286)
            at android.widget.ArrayAdapter.add(ArrayAdapter.java:182)
            at xxxxx.LocateActivity$2.didRangeBeaconsInRegion(LocateActivity.java:84)
            at org.altbeacon.beacon.BeaconIntentProcessor.onHandleIntent(BeaconIntentProcessor.java:65)
            at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.os.HandlerThread.run(HandlerThread.java:61)
06-10 15:22:14.550    9781-9781/xxxxx D/BluetoothLeScanner﹕ could not find callback wrapper

My code

public class LocateActivity extends ActionBarActivity implements BeaconConsumer {

    public Button btnBack;
    protected static final String TAG = "RangingActivity";
    private BeaconManager beaconManager;
    private DevicesAdapter adapter;
    private Device newDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locate);

        beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.bind(this);

        // Construct the data source
        ArrayList<Device> arrayOfDevices = new ArrayList<Device>();
        // Create the adapter to convert the array to views
        adapter = new DevicesAdapter(this, arrayOfDevices);
        // Attach the adapter to a ListView
        ListView listView = (ListView) findViewById(R.id.listViewDevice);
        listView.setAdapter(adapter);



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }
    @Override
    public void onBeaconServiceConnect() {
        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() > 0) {
                    Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");


                    // Add item to adapter
                    newDevice = new Device("Test", beacons.iterator().next().getDistance() + "meter(s)");
                    //newDevice = new Device("Tom", 3.0);
                    adapter.add(newDevice);

                }
            }
        });

        try {
            beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
        } catch (RemoteException e) {    }
    }

}

Please advice. Thank you.


Source: java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.