Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Following Code Display attached Deveice in List,But it not showing the Devices.
Java
Showing Message Exception : null;
	@Override
	protected void onNewIntent(Intent intent) {
		try
		{
			UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
			HashMap<string,> deviceList = manager.getDeviceList();
			Iterator<usbdevice> deviceIterator = deviceList.values()
					.iterator();
			while (deviceIterator.hasNext()) {
				UsbDevice device = deviceIterator.next();
				if(device==null)
					return ;
				String deviceName = device.getDeviceName();
				listDeviceName.add(deviceName);
			}

			/*runOnUiThread(new Runnable()
			{
				@Override
				public void run() {*/
					
					final ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1, listDeviceName);
					listview.setAdapter(adapter);
			/*	}
			});*/
		}
		catch(Exception e)
		{
			Toast.makeText(this,"Exception :"+e.getMessage(),Toast.LENGTH_SHORT).show();
		}
	}

}
Posted
Updated 1-Jan-15 19:23pm
v2

Java
if(device==null)
    return ;

is wrong. You should not return at this point but use continue to repeat the while loop, until hasNext returns false. You will then fall out of the loop and display the listview.
 
Share this answer
 
Quick glance and I would say

Java
if(device==null)
   return ;


Needs to be

Java
if(device==null)
   continue ;


/Darren

Edit: When posted no answer was on this so I don't know what happened but at least I got the same answer correct :-)
 
Share this answer
 
v2
Comments
Richard MacCutchan 2-Jan-15 11:51am    
On the contrary, my answer was posted 2 hours before yours.
Darren_vms 2-Jan-15 12:54pm    
I know and I don't know what happened, when I looked the post was unanswered I went to mock up the code and noticed the return; so I posted. When I came back to look at the item your post was hours before mine so it made my post look dumb... perhaps I should refresh the tab just to make sure no one has answered in between.
Richard MacCutchan 5-Jan-15 6:48am    
I should not worry about it, it's happened to me once or twice. Probably due to caching in the server farm.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900