Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By kjkrum

Thread: Cast subclass to superclass problem

  1. #1
    emiliosa is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Cast subclass to superclass problem

    Hi, im new here, hope i learn more to help other people in future. but now i have this problem:

    Java Code:
    public class CameraFragment extends Fragment {
    public CameraFragment() {
    super();
    }}
    public class InscripcionJugadoresFragment extends CameraFragment {
    public InscripcionJugadoresFragment() {
    super();
    }}
    And i want to do on main activity:

    Java Code:
    @Override
    public Fragment getItem(int position) {
    ...
    CameraFragment cameraFragment = new CameraFragment();
    ...
    cameraFragment = new InscripcionJugadoresFragment();
    ...
    return (Fragment) cameraFragment;
    }
    Why do i get error "Cannot cast from CameraFragment to Fragment" on return? What am i missing? Am i so stupid?

    Thanks in advance.-
    Last edited by emiliosa; 03-12-2014 at 02:45 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Cast subclass to superclass problem

    Where is your Fragment class? Please submit a small, fully compilable program that demonstrates the problem.
    Also, I just noticed this is in the Android forum. If this is simply Java specific, I can help. If it is related to Android,
    I probably won't be able to.

    Regards,
    Jim
    Last edited by jim829; 03-12-2014 at 03:15 AM.
    emiliosa likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Cast subclass to superclass problem

    The only thing I can think of is that Fragment refers to two different classes in InscripcionJugadoresFragment and MainActivity. This could happen because one is using the Fragment from the main API and the other is using the Fragment from the compatibility library. Check your import statements and make sure they match.
    emiliosa likes this.
    Get in the habit of using standard Java naming conventions!

  4. #4
    emiliosa is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: Cast subclass to superclass problem

    In fact you are on right way kjkrum, i puted wrong imports, now i works as should. Thanks for help and sorry for late response.

Similar Threads

  1. subclass arguments to superclass variables
    By Gatsu in forum New To Java
    Replies: 1
    Last Post: 01-23-2013, 04:48 PM
  2. Superclass and Subclass Constructor Question
    By achiu17 in forum New To Java
    Replies: 1
    Last Post: 01-30-2012, 05:05 AM
  3. subclass calling superclass method
    By stackptr89 in forum New To Java
    Replies: 10
    Last Post: 11-22-2010, 07:40 PM
  4. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 PM

Posting Permissions

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