1

I am currently using the predefined navigation type "Tabs + swipe" (initially chosen when I created my project).

I have three tabs and I need two listview and one MapActivity. I succedded in displaying listview thanks to FragmentList but now I am trying to display a MapActivity and my code doesn't work at all :/

Here is what I added:

public static class MyFragment extends Fragment {
        int idList;

        private MapView map=null;
        private MyLocationOverlay me=null;

        static MyFragment newInstance(int num) {
            MyFragment f = new MyFragment();

            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);

            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            return(new FrameLayout(getActivity()));
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            map = new MapView(getActivity(), "xxxxxxxxxx");
            map.setClickable(true);

            Drawable marker=getResources().getDrawable(R.drawable.ic_action_search);
            marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());

            map.getOverlays().add(new ItemizedOverlay(marker, getActivity()));

            me = new MyLocationOverlay(getActivity(), map);
            map.getOverlays().add(me);

            ((ViewGroup)getView()).addView(map);
        }
    }

Into getItem(int) in my FragmentPagerAdapter I do this:

public Fragment getItem(int position) {
    if(position == 0)
       return MyFragment.newInstance(0);
}

And then I got this error:

10-02 18:28:41.618: E/AndroidRuntime(23677): FATAL EXCEPTION: main
10-02 18:28:41.618: E/AndroidRuntime(23677): java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
10-02 18:28:41.618: E/AndroidRuntime(23677):    at com.google.android.maps.MapView.<init>(MapView.java:291)
10-02 18:28:41.618: E/AndroidRuntime(23677):    at com.google.android.maps.MapView.<init>(MapView.java:235)

Any idea about what should I do?

Taruni
  • 1,671
  • 4
  • 22
  • 43
vital
  • 1,308
  • 3
  • 13
  • 28
  • Take a look at this link. It has the solution along with a working example. https://stackoverflow.com/questions/5109336/mapview-in-a-fragment-honeycomb – Antrromet Oct 02 '12 at 16:48
  • When you drop a link, you should give it some explanation instead of just hoping the OP can figure it out. – Cat Oct 02 '12 at 17:17
  • Yes, some explanations would be convenient :) I tried to use the solution by adding the corresponding files and by doing MyFragment extends MyMapFragment (instead of Fragment) but I am still getting the same error :( – vital Oct 02 '12 at 17:26
  • My bad, it works great, I just made a mistake. Finally, it was very simple, thanks :) – vital Oct 02 '12 at 21:56
  • Which one out off all the answers is it in the link? – Totic Apr 01 '14 at 17:00
  • @Totic The first solution is the accepted one. But the 2nd one has more upvotes. Both are correct answers. – Antrromet Apr 02 '14 at 05:28

0 Answers0