1

I am referring to this video tutorial here ---- CLICK HERE


I the video the speaker is using a code below


He is using dynamic way of dealing with fragments

MainActivity.java

package com.example.simpledynamicfragment;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentManager;
import android.app.Fragment;

import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        FragmentActivity frag=new FragmentActivity();
        FragmentManager FM = getSupportFragmentManager();       
        
        
    }


}

I am getting the error at line ::

FragmentManager FM = getSupportFragmentManager();
  • I know we need to extend fragment class but in the video the speaker uses extending the activity. How is he accomplishing the task without encountering errors.

Or

  • am i doing the mistake

Note:: I am using support package for fragments

ERROR:: The method getSupportFragmentManager() is undefined for the type MainActivity

How to clarify & clear my doubts .... Any guidance would e helpful

Community
  • 1
  • 1
smriti3
  • 871
  • 2
  • 15
  • 35
  • what error do you get? Using an activity as host for the fragments is correct so far. And why not extending with FragmentActivity? – bofredo Oct 31 '13 at 11:03
  • @ bofredo ...... I get the error as ..... The method getSupportFragmentManager() is undefined for the type MainActivity – smriti3 Oct 31 '13 at 11:05
  • have you tried changing the extending to FragmentActivity? – bofredo Oct 31 '13 at 11:07
  • I am trying it now ... Please check that video link .... how speaker is accomplishing that – smriti3 Oct 31 '13 at 11:12
  • Also do we need fragmentActivity for backward compatibility .... speaker in the video looks he is using API >11 ... i am using lower level API – smriti3 Oct 31 '13 at 11:14
  • Speaker not build project, if he build and run — got same error. – artemiygrn Oct 31 '13 at 11:17
  • adding the package android.support.v4.app.FragmentActivity .... cleared the error – smriti3 Oct 31 '13 at 11:22

1 Answers1

4

Please, replace extends Activity to extends FragmentActivity, this help.

EDIT:

Speaker on video not build project, if he build and run, he will got same error.

artemiygrn
  • 1,036
  • 7
  • 18
  • Can't we use extends Activity to host fragments ..... please check that video in the tutorial ? – smriti3 Oct 31 '13 at 11:07
  • 3
    It may be error, you try use method **getSupportFragmentManager** which located only in **FragmentActivity**, look this link: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html – artemiygrn Oct 31 '13 at 11:11
  • 1
    I think u need Fragment Activity for backward compatibility issues. Moreover you have more functionality. Check this StackO [post](http://stackoverflow.com/questions/15318518/android-activity-vs-fragmentactivity – Poutrathor Oct 31 '13 at 11:12
  • @kvirair ..... I cleared the error ... the support package ....android.support.v4.app.FragmentActivity .... was missing from the class here .... your link helped me cleared the error ..... May be the speaker might be using API>11 ... so he might not got the error – smriti3 Oct 31 '13 at 11:20
  • the guy in the tutorial doesn't use the support-library. i guess that is the reason why he can extend just with the "Activity". – bofredo Oct 31 '13 at 11:21
  • @smriti3 Framgents are from api level 11. if you need backward compatibility use `SupportFragment` in which case you need to extend FragmentActivity. If you min sdk is 11 and above use Fragment and extend Activity – Raghunandan Oct 31 '13 at 11:28
  • @ Raghunandan ..... Yes(my APK is below 11) but still we can use extend activity ..... But we have to import android.support.v4.app.FragmentActivity .... i checked and it works like this ..... also http://stackoverflow.com/questions/15318518/android-activity-vs-fragmentactivity link helped me figure it out :) – smriti3 Oct 31 '13 at 11:37
  • 1
    @ Raghunandan ..... Because android.app.Activity ---- extends------ android.support.v4.app.FragmentActivity – smriti3 Oct 31 '13 at 11:38