FragmentActivity,ViewPager和MapActivity

时间:2012-10-02 16:44:17

标签: android android-mapview fragment

我目前正在使用预定义的导航类型“Tabs + swipe”(最初在我创建项目时选择)。

我有三个标签,我需要两个listview和一个MapActivity。 感谢FragmentList,我成功地显示了listview,但现在我正在尝试显示MapActivity,而我的代码根本不起作用:/

以下是我添加的内容:

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);
        }
    }

我的FragmentPagerAdapter中的getItem(int)我这样做:

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

然后我收到了这个错误:

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)

我应该怎么做?

1 个答案:

答案 0 :(得分:2)

看一下这个链接。它有一个解决方案和一个工作示例。 MapView in a Fragment (Honeycomb)