Results 1 to 6 of 6
  1. #1
    AedonetLIRA is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default Class cast issue

    I have a series of classes (Analog/Digital) extended from GaugeBase. I want GaugeBase to run in the background on a service (Android) and when the gauge needs to be displayed, cast it into the appropriate skin. However, I keep getting this error:

    Java Code:
    11-18 17:10:37.526: WARN/System.err(7946): java.lang.ClassCastException: appion.userface.GaugeComponents.GaugeBase
    11-18 17:10:37.526: WARN/System.err(7946):     at MainUserWorkArea.WorkBench$WorkBenchAdapter.add(WorkBench.java:121)
    11-18 17:10:37.526: WARN/System.err(7946):     at MainUserWorkArea.WorkBench.addTool(WorkBench.java:67)
    11-18 17:10:37.526: WARN/System.err(7946):     at appion.userface.activities.Controller.onCreate(Controller.java:53)
    11-18 17:10:37.526: WARN/System.err(7946):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
    11-18 17:10:37.526: WARN/System.err(7946):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
    11-18 17:10:37.526: WARN/System.err(7946):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
    11-18 17:10:37.526: WARN/System.err(7946):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
    11-18 17:10:37.526: WARN/System.err(7946):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
    11-18 17:10:37.526: WARN/System.err(7946):     at android.os.Handler.dispatchMessage(Handler.java:99)
    11-18 17:10:37.526: WARN/System.err(7946):     at android.os.Looper.loop(Looper.java:144)
    11-18 17:10:37.526: WARN/System.err(7946):     at android.app.ActivityThread.main(ActivityThread.java:4937)
    11-18 17:10:37.526: WARN/System.err(7946):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-18 17:10:37.526: WARN/System.err(7946):     at java.lang.reflect.Method.invoke(Method.java:521)
    11-18 17:10:37.526: WARN/System.err(7946):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    11-18 17:10:37.526: WARN/System.err(7946):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    11-18 17:10:37.526: WARN/System.err(7946):     at dalvik.system.NativeStart.main(Native Method)
    And my code being:

    Java Code:
    public boolean add(int t, long id){
    	    	if (getCount() > 2) return false;
    	    	double size = (mWidth < mHeight) ? mWidth * .40 : mHeight * .40;
    	    	GaugeBase gb = new GaugeBase(context, dSM, mSP, (int)size, id);
    	    	
    	    	switch (t) {
    	    	case ANALOG:
    	    		Log.w(TAG, "SWITCHING IN ADAPTER: ANALOG");
    	    		Analog at = (Analog)gb;
    	    		at.init();
    	    		at.postInvalidate();
    	    		mToolBox.add(at);
    	    		return true;
    	    	case DIGITAL:
    	    		try {
    	    		Log.w(TAG, "SWITCHING IN ADAPTER: DIGITAL");
    	    		Digital dt = (Digital)gb;  // LINE 121//
    	    		dt.init();
    	    		mToolBox.add(dt); }catch(Exception e) { e.printStackTrace();}
    	    		return true;
    	    	}
    	    	return false;
    	    }
    I'm going to keep looking at it, but any help will be appreciated.
    ~Aedon
    Last edited by AedonetLIRA; 11-19-2010 at 12:22 AM. Reason: Typo

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    I am not sure, I never used this before but I think the error is this:
    Java Code:
        Analog at = (Analog)gb;
    GaugeBase cannot be cast to Analog.

    See this: http://download.oracle.com/javase/1....rticle-08.html
    Last edited by mine0926; 11-19-2010 at 12:57 AM. Reason: EDIT THE URL

  3. #3
    AedonetLIRA is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    This may be true, but as I understand it, it only breaks if the hierarchy is not a linear progression from super class to sub class. In my instance it is a direct lineage. Analog/Digital directly extends GaugeBase. Am I wrong in how I am understanding this?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AedonetLIRA View Post
    This may be true, but as I understand it, it only breaks if the hierarchy is not a linear progression from super class to sub class. In my instance it is a direct lineage. Analog/Digital directly extends GaugeBase. Am I wrong in how I am understanding this?
    Yep, you're wrong; a derived class D is a base class B; the reverse isn't true; you have created an object B and you want to use it as if it were an object D.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    AedonetLIRA is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    I see.
    On that note, how would I make this work? I have a structure which needs to support the service and processing of the gauge, an a GUI (the analog/digital extensions) to attach to the service. My thinking, flawed as it now seems, was to create the service and when a GUI is needed to just cast the GaugeBase into it.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AedonetLIRA View Post
    I see.
    On that note, how would I make this work? I have a structure which needs to support the service and processing of the gauge, an a GUI (the analog/digital extensions) to attach to the service. My thinking, flawed as it now seems, was to create the service and when a GUI is needed to just cast the GaugeBase into it.
    Maybe you can create an object of the correct class in each case block?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Need Help with Class Cast Exception
    By soxfan714 in forum New To Java
    Replies: 2
    Last Post: 11-03-2011, 04:33 PM
  2. Cast object to custom class problem
    By trader5050 in forum New To Java
    Replies: 7
    Last Post: 11-15-2010, 10:47 AM
  3. Cast own class to FrameView
    By Luxpsycho in forum AWT / Swing
    Replies: 0
    Last Post: 07-16-2010, 10:13 AM
  4. class cast exception
    By venkatallu in forum New To Java
    Replies: 2
    Last Post: 09-02-2008, 09:50 PM
  5. Replies: 1
    Last Post: 08-02-2007, 04:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •