Casting problem for Phidget and InterfaceKitPhidget

Supporting 4.1 and up
blainablin
Phidgetsian
Posts: 14
Joined: Tue Oct 09, 2012 7:27 pm
Location: Caledonia, Ontario
Contact:

Casting problem for Phidget and InterfaceKitPhidget

Postby blainablin » Sun Dec 09, 2012 11:51 am

I'm running into a casting problem when I try to cast from a Phidget object to InterfaceKitPhidget object. In the docs it says InterfaceKitPhidget is a subclass of Phidget. I'm new to Java but as I understand it casting from superclass to subclass should be allowed.

I'm using Manager for the attach/detach events and have set up the Attach Event Handler.

Code: Select all

          phManager.addAttachListener(new AttachListener() {
            public void attached(final AttachEvent attachEvent) {
               AttachEventHandler(attachEvent.getSource());
            }
           });
 


In the Handler I have:

Code: Select all

void AttachEventHandler(Phidget device) {

   phidgetSerial = device.getSerialNumber();

   if (device.getDeviceClass()==7) {    // 7 is Interface Kit
      ik = (InterfaceKitPhidget)device;
      ik.open(phidgetSerial);

}


But it's giving me a cast error:

Code: Select all


12-09 12:22:14.291  4048  4062 W dalvikvm: threadid=11: thread exiting with uncaught exception (group=0x409f71f8)

12-09 12:22:14.291  4048  4062 E AndroidRuntime: FATAL EXCEPTION: Thread-403

12-09 12:22:14.291  4048  4062 E AndroidRuntime: java.lang.ClassCastException: com.phidgets.Phidget cannot be cast to com.phidgets.InterfaceKitPhidget

12-09 12:22:14.291  4048  4062 E AndroidRuntime:    at com.Goats.WeighScale.WeighScaleActivity.AttachEventHandler(WeighScaleActivity.java:138)

12-09 12:22:14.291  4048  4062 E AndroidRuntime:    at com.Goats.WeighScale.WeighScaleActivity$1.attached(WeighScaleActivity.java:67)

12-09 12:22:14.291  4048  4062 E AndroidRuntime:    at com.phidgets.Manager.fireAttach(Manager.java:245)

12-09 12:22:14.291  4048  4062 E AndroidRuntime:    at dalvik.system.NativeStart.run(Native Method)

12-09 12:22:14.291   130   375 W ActivityManager:   Force finishing activity com.Goats.WeighScale/.WeighScaleActivity



What am I doing wrong? Thanks in advance.
Windows XP, Nextbook Premium 8 SE, Android 4.0.3, InterfaceKit 8/8/8, Load cells

erik
King of the Lab
Posts: 476
Joined: Fri Mar 06, 2009 12:42 pm
Location: Calgary, Canada
Contact:

Re: Casting problem for Phidget and InterfaceKitPhidget

Postby erik » Mon Dec 10, 2012 8:57 am

What is "ik" initialized as? It should be

Code: Select all

private InterfaceKitPhidget ik = new InterfaceKitPhidget();


If ik is initialized as simply a Phidget, this might be why it is failing the cast.

blainablin
Phidgetsian
Posts: 14
Joined: Tue Oct 09, 2012 7:27 pm
Location: Caledonia, Ontario
Contact:

Re: Casting problem for Phidget and InterfaceKitPhidget

Postby blainablin » Mon Dec 10, 2012 10:23 am

erik wrote:What is "ik" initialized as? It should be

Code: Select all

private InterfaceKitPhidget ik = new InterfaceKitPhidget();


If ik is initialized as simply a Phidget, this might be why it is failing the cast.



It is initialized exactly how you have it.

It's a bit frustrating because in the docs it states the getSource() method provides a Phidget object that can be cast.

I was wondering if anyone else came across this issue.
Windows XP, Nextbook Premium 8 SE, Android 4.0.3, InterfaceKit 8/8/8, Load cells

User avatar
Patrick
Lead Developer
Posts: 3049
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Casting problem for Phidget and InterfaceKitPhidget

Postby Patrick » Mon Dec 10, 2012 10:44 am

That documentation is for a Phidget attach event - not a PhidgetManager attach event. You cannot call .open() directly on a PhidgetManager object - these are just simple Phidget objects to be used to retrieve serial number, device type, etc. You need to create a new PhidgetInterfaceKit object and call open on it using the serial number from the Manager object.

If you register an attach event on the new interfacekit object before calling open, then in that attach event, you will be able to cast the source into an InterfaceKit - because this is what the original object really is.

Also, if you are just opening an interface kit, you may want to consider if you need to use the Manager at all - this is generally just used to get a listing of attached devices.

-Patrick

blainablin
Phidgetsian
Posts: 14
Joined: Tue Oct 09, 2012 7:27 pm
Location: Caledonia, Ontario
Contact:

Re: Casting problem for Phidget and InterfaceKitPhidget

Postby blainablin » Mon Dec 10, 2012 4:14 pm

Thanks Patrick.

I've changed my code to get the serial number via the Manager and then open the InterfaceKitPhidget with that number.

Now I'm running into another problem but I'll start a new topic for that.

Thanks
Blain
Windows XP, Nextbook Premium 8 SE, Android 4.0.3, InterfaceKit 8/8/8, Load cells


Return to “Android”

Who is online

Users browsing this forum: No registered users and 1 guest