谷歌地图片段:在ViewPager中销毁和重新创建时,inflater.inflate会崩溃?

时间:2013-05-14 01:43:55

标签: android android-viewpager android-fragmentactivity android-tabs

我正在使用actionbarsherlock及其sherlockFragmentActivity。

我有3个片段,并在Tab + ViewPager布局中。

第3个片段是问题所在。这是一个在谷歌地图上显示我的位置的片段。

从我注意到,viewpager,将创建新页面,如果我们滚动页面超过1页,那么它将DESTORY该页面。

所以第一次这一切都很好,我转到第2页,它使用GOOGLEMAPS在第3页调用CREATE。

然而,问题是如果我回到第1页(DESTORIES第3页googlemaps)。现在,如果我回到第2页(尝试创建第3页),第3页的inflater.inflate将会DIE,我将收到错误,首先是Choreographer类。

所以我的问题是,为什么inflater.inflate失败了,用这样的片段编写标签viewpager的正确方法是什么。???

此处的代码(创建主要活动):

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main_activity);
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();

        mViewPager = (ViewPager)findViewById(R.id.pager);

        mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

        mTabsAdapter.addTab(mTabHost.newTabSpec("driverInfo").setIndicator("Driver Info"),
                DriverInfo.class, null);
        mTabsAdapter.addTab(mTabHost.newTabSpec("alarmSystem").setIndicator("Alarm System"),
                AlarmSetting.class, null);
        mTabsAdapter.addTab(mTabHost.newTabSpec("vehicleLocation").setIndicator("Location"),
                VehicleLocation.class, null);


        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }

代码:位置片段:

public class VehicleLocation extends Fragment implements ServerRequestInterface {

    private GoogleMap mMap; 
    private ProgressDialog mProgressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try
        {
            super.onCreate(savedInstanceState);
            setupProgressDialog();
        }
        catch (Exception ex)
        {
            System.out.println(ex.toString());
        }


}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//WHere it keeps die-ing     ----------------------------------    
    View view = inflater.inflate(R.layout.vehicle_location, container, false);
            setUpMapIfNeeded();
            return view;
        }

错误日志:

05-13 20:48:31.532: E/AndroidRuntime(13219): FATAL EXCEPTION: main
05-13 20:48:31.532: E/AndroidRuntime(13219): android.view.InflateException: Binary XML file line #2: Error inflating class fragment
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at com.covixon.main.VehicleLocation.onCreateView(VehicleLocation.java:37)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.view.ViewPager$3.run(ViewPager.java:238)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.view.Choreographer.doFrame(Choreographer.java:531)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.os.Handler.handleCallback(Handler.java:725)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.os.Looper.loop(Looper.java:137)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.app.ActivityThread.main(ActivityThread.java:5237)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at java.lang.reflect.Method.invoke(Method.java:511)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at dalvik.system.NativeStart.main(Native Method)
05-13 20:48:31.532: E/AndroidRuntime(13219): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f040066, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
05-13 20:48:31.532: E/AndroidRuntime(13219):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
05-13 20:48:31.532: E/AndroidRuntime(13219):    ... 26 more

1 个答案:

答案 0 :(得分:2)

在更多地盯着堆栈跟踪之后,我注意到了这一行

 Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f040066, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment

这引导我谷歌这样,这解决了我的问题:

Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment