-1

Here is the class of fragment

package com.hfad.chapter7;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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 setWorkoutID(long workoutID) {
        this.workoutID = workoutID;
    }
}

And the layout it's using is:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.hfad.chapter7.WorkoutDetailFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

My MainActivity is:

package com.hfad.chapter7;

import android.app.Activity;
import android.app.Fragment;
import android.provider.UserDictionary;
import android.support.v4.app.FragmentActivity;
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);

        WokoutDetailFragment workoutDetailFragment;
        workoutDetailFragment = (WorkoutDetailFragment)this.getFragmentManager().findFragmentById(R.id.myFirstFragment);


    }
}

And layout of MainActivity is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hfad.chapter7.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />

    <fragment
        class="com.hfad.chapter7.WorkoutDetailFragment"
        android:id="@+id/myFirstFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />



</LinearLayout>

Problem is when I use this.getFragmentManager().findFragmentById(R.id.myFirstFragment) in onCreate of AppCompatActivity I get Fragment though I should get WorkoutDetailFragment as stated here in a similar example.

UPDATE:

Also I am unable to cast Fragment to WorkDetailFragment enter image description here

Community
  • 1
  • 1
user4913383
  • 171
  • 1
  • 1
  • 11
  • 1
    Have you tried `getSupportFragmentManager()` instead? Or does your Fragment class not extend the v4 Fragment? – OneCricketeer May 10 '16 at 11:41
  • Please include the imports for your Fragment and someone will explain why you can't cast – OneCricketeer May 10 '16 at 11:47
  • @cricket_007 Thanks a lot, using `getSupportFragmentManager() fixed the problem. Would be great if you can point to some resource or explain why did it work? – user4913383 May 10 '16 at 11:48
  • 1
    It's confusing, but there are several `Fragment` classes, you have to import the right one. Read the other answers in the post you linked to. (the second part of this one). http://stackoverflow.com/a/25416259/2308683 – OneCricketeer May 10 '16 at 11:51

3 Answers3

1

@user4913383,

Change this line:

workoutDetailFragment = (WorkoutDetailFragment)this.getFragmentManager().findFragmentById(R.id.myFirstFragment);

to

workoutDetailFragment = (WorkoutDetailFragment)this.getSupportFragmentManager().findFragmentById(R.id.myFirstFragment);
0

What's the problem of getting a fragment? The getFragmentManager().findFragmentById() method returns a Fragment and you have to cast it. Since you are casting the Fragment to a WorkoutDetailFragment you should be able to do what you want with it.

Lucas P.
  • 4,282
  • 4
  • 29
  • 50
0

I think it has to be <fragment android:name="com.hfad.chapter7.WorkoutDetailFragment"... instead of class. => http://developer.android.com/training/basics/fragments/creating.html#AddInLayout

Micky
  • 5,578
  • 7
  • 31
  • 55