AndroidUpnpService variable gets Null Pointer Exception on Android

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

AndroidUpnpService variable gets Null Pointer Exception on Android

xlehoc01
This post was updated on .
Hi
I got problem with AndroidUpnpService upnpService variable. My code is nearly same as the Demo app. Here it is:

public class MainActivity extends Activity {
    private ListView upnpList; 
    private Button rescan;
		
    private ArrayAdapter<DeviceDisplay> listAdapter;

    private BrowseRegistryListener registryListener = new BrowseRegistryListener();

    private AndroidUpnpService upnpService;
    
    private ServiceConnection serviceConnection = new ServiceConnection() { 
        public void onServiceConnected(ComponentName className, IBinder service) {
        	      	
            upnpService = (AndroidUpnpService) service;

            // Clear the list
            listAdapter.clear();

            // Get ready for future device advertisements
            upnpService.getRegistry().addListener(registryListener);

            // Now add all devices to the list we already know about
            for (Device device : upnpService.getRegistry().getDevices()) {
                registryListener.deviceAdded(device);
            }

            // Search asynchronously for all devices, they will respond soon
            upnpService.getControlPoint().search();
            
        }
   }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        upnpList = (ListView)this.findViewById(R.id.foundList);
               
        listAdapter = new ArrayAdapter<DeviceDisplay>(this, android.R.layout.simple_list_item_1);
        upnpList.setAdapter(listAdapter);

        getApplicationContext().bindService(new Intent(this, AndroidUpnpServiceImpl.class), serviceConnection, Context.BIND_AUTO_CREATE);
       
        rescan = (Button)findViewById(R.id.rescanBtn);
        rescan.setOnClickListener(new View.OnClickListener() {
			
		@Override
			public void onClick(View v) {
				refreshList();	
			}
		});	
        
        upnpList.invalidate();
    }

    private void refreshList(){
               
        if (upnpService == null){ // <--------------------HERE IT GETS NULL
        	return;
        }
        
        upnpService.getRegistry().removeAllRemoteDevices();
        upnpService.getControlPoint().search();

    }
    
    .... same from demo application ....

}

When pressing the refresh button the refreshList method returns because the upnpService variable is null.
Can anybody tell me why is this variable uninitialized?
Reply | Threaded
Open this post in threaded view
|

Re: AndroidUpnpService variable gets Null Pointer Exception on Android

xlehoc01

Somehow the "new ServiceConnection()" doesen't works. it's onServiceConnected() method is not called. I tested the demo application, but I can't see this method called eather.
Reply | Threaded
Open this post in threaded view
|

Re: AndroidUpnpService variable gets Null Pointer Exception on Android

DREDD112
In reply to this post by xlehoc01
The service won't run if you do not declare it in your manifest.