简体   繁体   中英

Wrong 2nd argument type with fragment

Reading "Head First Android Programming", it's kinda outdated, so code which is shown in this book is also outdated. My Code

Android Studio says: Wrong 2nd argument type. Found: com.xfunny.workout.WorkoutDetailFragment', required: android.app.Fragment'

public class WorkoutDetailFragment extends Fragment {

private long workoutId;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_workout_detail, container, false);
}

@Override
public void onStart() {
    super.onStart();
    View view = getView();
    if(view != null){
        TextView title = (TextView) view.findViewById(R.id.textTitle);
        Workout workout = Workout.workouts[(int)workoutId];
        title.setText(workout.getName());
        TextView description = (TextView) view.findViewById(R.id.textDescription);
        description.setText(workout.getDescription());
    }
}

public void setWorkout(long id){
    this.workoutId = id;
}

}

Do somthing like this

android.app.Fragment details = new WorkoutDetailFragment();

instead of

WorkoutDetailFragment details = new WorkoutDetailFragment();

Since you are using the AppCompatActivity you need to use the Fragment from the support library, ie import android.support.v4.app.Fragment

And of course if you switch to the support Fragment, you will need to use getSupportFragmentManager() instead of getFragmentManager()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM