0

滑动到 fragment_right 然后回到fragment_middle 时出现问题。如果我只从 fragment_middle 向 fragment_left 滑动,反之亦然,一切正常。

我正在使用带有 ViewPagerAdapter 的 FragmentActivity 和 ViewPager。到目前为止,我有三个 Fragment。

片段左片段中间片段右

fragment_left 的谷歌地图视图如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_left"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/color_lime" >

    <RelativeLayout
        android:id="@+id/map_map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:background="@color/color_red" >

        <fragment 
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/map_chat"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/color_white" >

        <TextView
            android:id="@+id/map_tv_stupid_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="fragment left"
            android:textSize="18sp" />
    </RelativeLayout>

</LinearLayout>

这是我的 FragmentLeft 课程:

package com.example.worldfriends;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentLeft extends Fragment {

    public static Fragment newInstance(Context context) {
        FragmentLeft fragment = new FragmentLeft();

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_left, null);
        return view;
    }

}

这是我的 ViewPagerAdapter:

package com.example.worldfriends;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private Context context;
    private static int totalPage=3;
    private int currentItem=1;

    public ViewPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        this.context=context;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new Fragment();
        switch(position){
        case 0:
            fragment=FragmentLeft.newInstance(context);
            break;
        case 1:
            fragment=FragmentMiddle.newInstance(context);
            break;
        case 2:
            fragment=FragmentRight.newInstance(context);
            break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return totalPage;
    }

    public int getCurrentItem() {
        return currentItem;
    }

}

如您所见,这是非常基本的。最后但并非最不重要的一点是堆栈跟踪!

10-29 17:52:09.951: W/dalvikvm(26552): threadid=1: thread exiting with uncaught exception (group=0x41985700)
10-29 17:52:09.975: E/AndroidRuntime(26552): FATAL EXCEPTION: main
10-29 17:52:09.975: E/AndroidRuntime(26552): android.view.InflateException: Binary XML file line #18: Error inflating class fragment
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at com.example.worldfriends.FragmentLeft.onCreateView(FragmentLeft.java:20)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1280)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.Choreographer.doFrame(Choreographer.java:531)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.os.Handler.handleCallback(Handler.java:730)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.os.Looper.loop(Looper.java:137)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.app.ActivityThread.main(ActivityThread.java:5103)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at java.lang.reflect.Method.invokeNative(Native Method)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at java.lang.reflect.Method.invoke(Method.java:525)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at dalvik.system.NativeStart.main(Native Method)
10-29 17:52:09.975: E/AndroidRuntime(26552): Caused by: java.lang.IllegalArgumentException: Binary XML file line #18: Duplicate id 0x7f060008, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
10-29 17:52:09.975: E/AndroidRuntime(26552):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
10-29 17:52:09.975: E/AndroidRuntime(26552):    ... 29 more
10-29 17:52:11.569: I/Process(26552): Sending signal. PID: 26552 SIG: 9
4

1 回答 1

1

原因:java.lang.IllegalArgumentException:二进制 XML 文件第 18 行:重复 id 0x7f060008、标签 null 或父 id 0x0 与 com.google.android.gms.maps.SupportMapFragment 的另一个片段

是你的问题。

这意味着您错误地创建了嵌套片段。阅读官方文档:不允许使用 xml,SupportMapFragment在代码中创建。

于 2013-10-29T21:14:53.293 回答