Android:使用ViewPager访问Fragment内部的Mapview

时间:2012-10-13 12:41:21

标签: android android-fragments android-mapview android-viewpager

我正在使用View Pager显示三个视图。

"Filter","Streets","Around"

每个视图都由片段定义。

我想要什么: 我想在我的FilterFragment中使用Mapview。

我做了什么:

我想使用MapView Inside Filter Fragment但无法成功运行。并获取日志错误:

android.view.InflateException: Binary XML file line #33: Error inflating class com.google.android.maps.MapView

我还搜索了关于此问题的Net和Stackoverflow并找到了LINKLINK但无法找到与我的问题相关的链接,因为我正在使用带有ViewPager的Fragment。

我把我的代码放在下面。

以下是MainViewPager.java的我的代码:

    package com.example.viewpageractivitydemo;

    import com.example.viewpageractivitydemo.viewpager.TitlePageIndicator;

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;
    import android.util.Log;

    public class MainViewPager extends FragmentActivity {

        TestFragmentAdapter adapter;
        ViewPager pager;

        @Override
        protected void onCreate(Bundle arg0) {
            // TODO Auto-generated method stub
            super.onCreate(arg0);
            Log.i("MainViewPager","Inside Oncreate");
            setContentView(R.layout.mainviewpager);
            pager = (ViewPager) findViewById(R.id.pager);

            adapter = new TestTitleFragmentAdapter(getSupportFragmentManager());
            pager.setAdapter(adapter);

            TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
            indicator.setViewPager(pager);
            pager.setCurrentItem(1);

        }

        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();

    //      pager.setCurrentItem(1);
        }

    }

这是TestFragmentAdapter.java的代码

package com.example.viewpageractivitydemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;

public class TestFragmentAdapter extends FragmentPagerAdapter {

    protected static final String[] CONTENT = new String[] { "Filter",
            "Streets", "Around" };
    public String TAG = "TestFragmentAdapter";
    private int mCount = CONTENT.length;

    public TestFragmentAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        Log.i(TAG, "Inside getItemMethod with position " + position);

        if (position == 0) {
            Log.i("TestFragmentAdapter","Inside if Condition");
            return FilterFragment.newInstance(position);
        } else if (position == 1) {
            return StreetsFragment.newInstance(position);
        } else if (position == 2) {
            return AroundFragment.newInstance(position);
        } else {
            return null;
        }
        // return MainFragment.newInstance(CONTENT[position % CONTENT.length]);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mCount;
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }

}

这是FilterFragment.java的我的代码:

package com.example.viewpageractivitydemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FilterFragment extends Fragment {

    /*
     * @Override protected Class<? extends Activity> getActivityClass() { //
     * TODO Auto-generated method stub return MyFilterActivity.class; }
     */

    public static Fragment newInstance(int position) {

        Log.i("FilterFragment","Inside New Instasnce");
        FilterFragment filterFragment = new FilterFragment();
        return filterFragment;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.i("FilterFragment","Inside OcCreateView");
        View view = inflater.inflate(R.layout.filter, null);
        return view;
    }

}

以下是filter.xml的我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#C6CDD4"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#93A4BA"
            android:gravity="center" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="5dp"
                android:gravity="center"
                android:text="Where Am I"
                android:textColor="#ffffff"
                android:textSize="18dp"
                android:textStyle="bold" >
            </TextView>
        </LinearLayout>

        <com.google.android.maps.MapView
            android:id="@+id/whereami_mapview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:apiKey="0-PaH6rk7RKrZSMd_lqdrZi6FazYyIlX5r6YFyA"
            android:clickable="true" />

        <!--
             <com.google.android.maps.MapView
            android:id="@+id/whereami_mapview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:apiKey="0-PaH6rk7RKrZSMd_lqdrZi6FazYyIlX5r6YFyA" />



        -->
    </LinearLayout>

</LinearLayout>

请有人建议我解决我的问题。我花了很多时间在这上面。仍然没有对我的问题有任何想法。 提前了。

2 个答案:

答案 0 :(得分:6)

您可以使用LocalActivityManager在片段中托管活动。这是一个弃用的类,但它提供了最简单的解决方案。

以下是代码中的片段代码(MyFilterFragment)。 YourMapActivity是一项扩展MapActivity的基本活动。

public class MapFragment extends Fragment {
    private static final String KEY_STATE_BUNDLE = "localActivityManagerState";

    private LocalActivityManager mLocalActivityManager;

    protected LocalActivityManager getLocalActivityManager() {
        return mLocalActivityManager;
    }

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

        Bundle state = null;
        if (savedInstanceState != null) {
            state = savedInstanceState.getBundle(KEY_STATE_BUNDLE);
        }

        mLocalActivityManager = new LocalActivityManager(getActivity(), true);
        mLocalActivityManager.dispatchCreate(state);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            //This is where you specify you activity class
        Intent i = new Intent(getActivity(), YourMapActivity.class); 
        Window w = mLocalActivityManager.startActivity("tag", i); 
        View currentView=w.getDecorView(); 
        currentView.setVisibility(View.VISIBLE); 
        currentView.setFocusableInTouchMode(true); 
        ((ViewGroup) currentView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        return currentView;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle(KEY_STATE_BUNDLE,
                mLocalActivityManager.saveInstanceState());
    }

    @Override
    public void onResume() {
        super.onResume();
        mLocalActivityManager.dispatchResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mLocalActivityManager.dispatchPause(getActivity().isFinishing());
    }

    @Override
    public void onStop() {
        super.onStop();
        mLocalActivityManager.dispatchStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLocalActivityManager.dispatchDestroy(getActivity().isFinishing());
    }
}

修改:由于此答案已发布,Google已发布Google地图Android API,并提供片段支持,并提供官方MapFragment课程。

答案 1 :(得分:0)

要使用Google地图,您必须扩展MapActivity,但您没有这样做。查看文档中的this example。本着可维护性的精神,我不会求助于使用已弃用功能的解决方案。

更可靠的解决方案是扩展MapActivity并从FragmentActivity复制缺少的实现。如果您不希望或需要向后兼容较旧的Android版本,请使用MapActivity而不是FragmentActivity,这应该已包含在Android 3.0及更高版本中显示Fragment所需的功能


修改:here's a project that did it for you