Mistake

I do everything according to the book, code:

MainActivity:

public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Fragment frag = getFragmentManager().findFragmentById(R.id.detail_frag); DetailFragment frag = (DetailFragment)getFragmentManager().findFragmentById(R.id.detail_frag); frag.setWorkout(1); } } 

DetailFragment: (this is another file class)

 public class WorkoutDetailFragment extends Fragment { private long workoutId; public WorkoutDetailFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_workout_detail, container, false); } public void setWorkout(long id){ this.workoutId = id; } } 

On -

 WorkoutDetailFragment frag = (WorkoutDetailFragment)getFragmentManager().findFragmentById(R.id.detail_frag); 

Text - Error: (16, 84) error: incompatible types: Fragment cannot be converted to WorkoutDetailFragment

  • what a mistake? what swears? what is the text of the error? - Grundy
  • On - WorkoutDetailFragment frag = (WorkoutDetailFragment) getFragmentManager (). FindFragmentById (R.id.detail_frag); Text - Error: (16, 84) error: incompatible types: Fragment cannot be converted to WorkoutDetailFragment - Vladimir Saleev
  • add it all to the question itself, the text itself - Grundy
  • Done, ps did not have time to read the answer - Vladimir Saleev
  • Check the full name of the class you are inheriting from - Grundy

2 answers 2

As prompted in the answer to a similar question

Your fragment should be inherited from android.support.v4.app.Fragment (perhaps a more current version), and not the usual Fragment class.

  • I just import it (or rather, the studio has already added it) - Vladimir Saleev
  • @ VladimirSaleev, where? in both files? need to check that extends and ghost then for identical classes goes - Grundy
  • I just casually looked at the import in the book. In DetailFragment you need to import android.app.Fragment, but not android.support.v4.app.Fragment - Vladimir Saleev

Android Studio imports android.support.v4.app.Fragment And you need - android.app.Fragment;