0

I have 3 fragments being used in a viewpager.I'm using a FragmentPagerAdapter to switch between them. I get this error all the time and I can't figure out why.

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ... make sure class name exists, is public, and has    an empty constructor that is public

Here's my getItem method. I have a static method to get the instance, but it still complains.

@Override
public Fragment getItem(int pos) {
  if (SECTION_COUNT > 1){
    switch(pos){
        case RECENT_HISTREES:
            return HistreesFragmentPhone.newInstance(HistreesViewType.Friends, accountId, userId);
        case MY_HISTREES:
            return HistreesFragmentPhone.newInstance(HistreesViewType.All, accountId, userId);
        case DOWNLOADED_HISTREES:
            return HistreesFragmentPhone.newInstance(HistreesViewType.Downloaded, accountId, userId);
    }
  }
   return HistreesFragmentPhone.newInstance(HistreesViewType.All, accountId, userId);
}

This is my Fragment constructor.

public static HistreesFragmentPhone newInstance(HistreesViewType viewType, int accountId, int userId) {
    HistreesFragmentPhone f = new HistreesFragmentPhone();

    Bundle args = new Bundle();
    args.putString("viewType", viewType.toString());
    args.putInt("accountId", accountId);
    args.putInt("userId", userId);
    f.setArguments(args);

    return f;
}
Eric Novins
  • 431
  • 3
  • 15
  • I haven't figured this out fully myself yet, but I think it's usually a "fake" exception in that there's some underlying exception that is the problem. The InstantiationException is a subclass of RuntimeException, and whenever I get a RuntimeException there's always some culprit underneath that's only visible in Logcat's full extended stack trace. What's frustrating is that I currently have an InstantiationException that rolls in from users, that I can't reproduce, and the stack trace Google gives me cuts the buttom off before I can get enough information! – Eric Mill Sep 27 '11 at 01:30
  • Another thing I'm trying, which contradicts my earlier comment - the error message for InstantiationException says to "make sure class name exists, is public, and has an empty constructor that is public". My inner static class wasn't marked as public, and did not have an empty public constructor. Maybe this is some strange edge case that only matters in some random circumstances? If this helps me, or it helps you, I'll post it as a formal answer on the thread here. – Eric Mill Sep 27 '11 at 01:39

0 Answers0