0

The line in MainActivity.java 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.

MainActivity

package gangster.workout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 WorkoutDetailFragment frag = (WorkoutDetailFragment) getFragmentManager().findFragmentById(R.id.detail_frag);
 frag.setWorkout(1);
}
}

Workout.Java

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){
 this.name = name;
 this.description = description;
}
public String getDescription(){
 return description;
}
public String getName(){
 return name;
}
public String toString(){
 return this.name;
}
}

WorkoutDetailFragment.java

package gangster.workout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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;
}
@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());
 }
}
@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);
}
}
asked May 31, 2016 at 20:27

1 Answer 1

1

use getSupportFragmentManager() instead of getFragmentManager()

answered May 31, 2016 at 20:29
Sign up to request clarification or add additional context in comments.

4 Comments

Dude, it works. Thank you. What was wrong in using that one though?
your code indicates that you're using android.support.v7.app.AppCompatActivity and android.support.v4.app.Fragment and both are from the android support library. which requires getting the correct FragmentManager by calling getSupportFragmentManager(). Please accept the answer since it solves your problem :)
I'm a beginner. Can you please explain a bit more un-sophisticatedly? :P I can accept the answer after 4 minutes.
The Android team offers a Support Library which provides backward-compatible versions of new features. for example Fragments were introduced in API v11, but using the support library you can use it in API v4 and up. you can read this part in the link I provided, you'll get the idea.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.