0

Similar question to TabHost with Fragments and FragmentActivity.

I have an Android application that has been set at API level 10 for years. I still have the minimum version at 10, but the max version is now 21. I've installed the Android v4 support JAR and I am converting everything from Activity to Fragment Activity.

So, I have a main screen with a tab host. The following code is used:

TabSpec spec = tabHost.newTabSpec("photos")
                      .setIndicator(getString(R.string.Photos),     
                         res.getDrawable(R.drawable.ics_menu_camera));
tabHost.addTab(spec, PhotosActivity.class, null);

The PhotosActivity used to extend the Activity class and now extends the FragmentActivity class. I simply get a cast error when the application opens. The only information is that PhotosActivity i what caused the cast error.

I tried changing this to extend Fragment instead (like shown in the link I referenced), but nearly every line of code became a massive error inside the PhotosActivity.

Community
  • 1
  • 1
Paul
  • 5,700
  • 5
  • 43
  • 67
  • The add() method expects that the class parameter to be a class representing a fragment and not an activity. *but nearly every line of code became a massive error inside the PhotosActivity* - you can't simply change the super class from activity to fragment, you'll need to refactor the current code of the activity to adapt it to a fragment class. – user Oct 29 '14 at 21:40
  • That's what I was afraid of. – Paul Oct 29 '14 at 21:46
  • There must be some way of doing this without an entire rewrite. getString(), getBaseContext(), findViewById(), getApplicationContext(), startActivityForResult, super, instance states, absolutely nothing exists in the Fragment class that exists in the Activity class. – Paul Oct 29 '14 at 21:55
  • No, there isn't. Anyway, the refactoring shouldn't be that hard after all. You can still use those methods simply by using the `getActivity()` method(returning the fragment's binding activity). – user Oct 30 '14 at 07:39
  • I'll begin work on all that. Can you post that as an answer and I will accept it? – Paul Oct 30 '14 at 13:37

1 Answers1

0

The PhotosActivity used to extend the Activity class and now extends the FragmentActivity class. I simply get a cast error when the application opens.

The cast exception happens because the add() method of the FragmentTabHost expects the class of a Fragment and not something that extends Activity. You can't simply make the transition between simple activities and fragments by making your activities to extend the Fragment class, instead you need to refactor your current classes.

user
  • 86,916
  • 18
  • 197
  • 190