Hello! Studying android on the book HeadFirst There was a problem

This is onCreate MainActivity

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WorkoutDetailFragment fragment = (WorkoutDetailFragment) getFragmentManager().findFragmentById(R.id.detail_frag); fragment.setWorkout(1); } 

This is the xml fragment:

 <fragment class="workout.hfad.com.workout.WorkoutDetailFragment" android:id="@+id/detail_frag" android:layout_width="0dp" android:layout_weight="3" android:layout_height="match_parent" /> 

Swears at the line

  <WorkoutDetailFragment fragment = (WorkoutDetailFragment) getFragmentManager().findFragmentById(R.id.detail_frag); 

And it gives an error:

  `Inconvertible types; cannot cast 'android.app.Fragment' to 'workout.hfad.com.workout.WorkoutDetailFragment' 

Actually, I did everything according to the book (sort of). Where is the mistake?

  • Apparently your WorkoutDetailFragment does not expand Fragment - YuriySPb
  • public class WorkoutDetailFragment extends Fragment Extends - Yegor Prokofiev
  • In general, the method of adding a fragment that you cited is bad, because it is buggy, inflexible, and, therefore, not used. Usually fragments are added programmatically. In this particular case, a search by the ID of the container in which the tag with the fragment is located can help instead of searching by the ID of the fragment tag itself. - Yuriy SPb
  • one
    So are you expanding android.app.Fragment or android.support.v4.app.Fragment ? If you use from support, then the Activity should be from the same place and getSupportFragmentManager() should be used. - woesss
  • Yes, the problem was that I expanded android.support.v4.app.Fragment. Thank! - Yegor Prokofiev

0