Vikrant Arora Vikrant Arora - 2 years ago 90
Android Question

Can you please guide me what's wrong in the code?

The line in in which WorkoutDetailFragment is casted shows error that it cannot be done. Please tell me why it isn't working. I copied the code from a book.


package gangster.workout;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
WorkoutDetailFragment frag = (WorkoutDetailFragment) getFragmentManager().findFragmentById(;


package gangster.workout;

public class Workout {
private String name;
private String description;

public static final Workout[] workouts = {
new Workout("The Limb Loosener" , " Pta nhi kya ksgdrna hai isme") ,
new Workout("The Timb Loosener" , " Pta nhi kya asdfasasfasasdkrna hai isme") ,
new Workout("The Pimb Loosener" , " Pta nhi kyaasf krna hai isme") ,
new Workout("The Zimb Loosener" , " Pta nhi kyasfasa krna hai isme") ,
private Workout(String name , String description){ = name;
this.description = description;

public String getDescription(){
return description;
public String getName(){
return name;
public String toString(){

package gangster.workout;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class WorkoutDetailFragment extends Fragment {
private long workoutId;

public void setWorkout(long id) {

this.workoutId = id;

public void onStart() {
View view = getView();
if (view != null) {
TextView title = (TextView) view.findViewById(;
Workout workout = Workout.workouts[(int) workoutId];
TextView description = (TextView) view.findViewById(;


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);


Answer Source

use getSupportFragmentManager() instead of getFragmentManager()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download