0

I am trying to create a SupportMapFragment inside a fragment..I get the following error: The method SupportFragmentManager() is undefined for the type MapFragment

my code is as follows:

import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class MapFragment extends Fragment{

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
       { 
         View v = inflater.inflate(R.layout.fragment_map_view, container, false);
         FragmentManager fm = getFragmentManager();
         FragmentTransaction ft = fm.beginTransaction();
         DateTimeFragment datetime=new DateTimeFragment();
         ft.add(R.id.datetime_container_map, datetime);
         SupportMapFragment mapFragment = (SupportMapFragment)SupportFragmentManager().findFragmentById(R.id.map_fragment);
         return v;
       }

}

However, SupportMapFragmentManager does not resolve. What is the solution to this problem?

gazubi
  • 561
  • 8
  • 32

2 Answers2

0

In addition to kalyan pvs's answer, your code is wrong, should look like this:

 SupportMapFragment mapFragment = (SupportMapFragment)getFragmentManager().findFragmentById(R.id.map_fragment);

In your question, you use the method SupportFragmentManager() which doesn't exists. The method getSupportFragmentManager is not available in Fragments, but getFragmentManager will work. This is I guess, because your class already extends the support version of Fragment and therefore getFragmentManager will automatically return the support version of the FragmentManager.

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • Hi, I had actually done mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment); in another class that extends FragmentActivity. And it works completely fine. However, it wasn't working when I did extends Fragment. I changed it to what you suggested and it resolved – gazubi Dec 30 '13 at 10:14
  • I changed my answer a bit. Just use getFragmentManager and you'll be fine. – fweigl Dec 30 '13 at 10:18
0

Check these couple of answers for the problem similar to yours by CommonsWare. Check Answer 1 and Answer 2.

Also, make sure you have Android Private Libraries checked under Properties->Java Build Path->Order and Export. Then clean the project and check if the issue is solved.

Community
  • 1
  • 1