在tablayout中加载片段时的Android InflateException

时间:2014-12-29 16:05:27

标签: java android xml eclipse

我有一个带有2个标签的tablayout。这是我在测试时所做的。 loding第一个标签,然后转到第二个标签,然后返回第一个标签。在这一点上我得到一个错误。但不知道如何解决它。我是这方面的初学者。我希望有人可以帮助我。

加载片段:https://www.dropbox.com/s/2zbmhisuwn0cezf/Screenshot_2014-12-29-17-00-27.png?dl=0 (所以在去约会后回到profiel,应用程序停止工作)

错误

12-29 16:41:25.959: E/AndroidRuntime(20992): FATAL EXCEPTION: main
12-29 16:41:25.959: E/AndroidRuntime(20992): Process: kb50.appointment, PID: 20992
12-29 16:41:25.959: E/AndroidRuntime(20992): android.view.InflateException: Binary XML file line #57: Error inflating class fragment
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at kb50.appointment.Profile.onCreateView(Profile.java:32)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1302)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:729)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at kb50.appointment.TabLayout.onTabChanged(TabLayout.java:132)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:453)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.widget.TabHost.setCurrentTab(TabHost.java:438)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:161)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:549)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.view.View.performClick(View.java:4756)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.view.View$PerformClick.run(View.java:19749)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.os.Handler.handleCallback(Handler.java:739)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.os.Looper.loop(Looper.java:135)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.app.ActivityThread.main(ActivityThread.java:5221)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at java.lang.reflect.Method.invoke(Native Method)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at java.lang.reflect.Method.invoke(Method.java:372)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-29 16:41:25.959: E/AndroidRuntime(20992): Caused by: java.lang.IllegalArgumentException: Binary XML file line #57: Duplicate id 0x7f060008, tag null, or parent id 0xffffffff with another fragment for kb50.appointment.ProfielListFragment
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2120)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.app.Activity.onCreateView(Activity.java:5282)
12-29 16:41:25.959: E/AndroidRuntime(20992):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
12-29 16:41:25.959: E/AndroidRuntime(20992):    ... 26 more
12-29 16:41:27.150: I/Process(20992): Sending signal. PID: 20992 SIG: 9

profiel.java

public class Profile extends Fragment{
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }


        return (LinearLayout)inflater.inflate(R.layout.profile_layout, container, false);
    }

profielListFragment.java(带有测试数据)

public class ProfielListFragment extends ListFragment {


    //temp data
    String[] nameApp = new String[] {
        "docter",
        "school"};
    String[] dateApp = new String[]{
        "2 september 2015",
        "3 september 2015"};
    String[] timeApp = new String[]{
            "9:00 pm",
            "8:00 am"};
    //end temp data

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

        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

        for(int i=0;i<2;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("name", "name : " + nameApp[i]);
            hm.put("date","date : " + dateApp[i]);
            hm.put("time","time : " + timeApp[i]);

            //hm.put("flag", Integer.toString(flags[i]) );
            aList.add(hm);
        }

        // Keys used in Hashmap
        String[] from = { "name","date","time" };
        // Ids of views in profiellistfragment_layout
        int[] to = { R.id.name,R.id.date,R.id.time};

        // Instantiating an adapter to store each items
        // R.layout.profiellistfragment_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.profiellistfragment_layout, from, to);

        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }


}

profile_layout.xml

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4"
        android:background="#0000FF"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"

            android:layout_gravity="top"
            android:adjustViewBounds="true"

            android:contentDescription="@string/ProfielPic"
            android:src="@drawable/pq" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" 
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:text="@string/userName"
                android:textColor="#FFFFFF"/>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:text="@string/userEmail"
                android:textColor="#FFFFFF"/>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/userPhone"
                android:textColor="#FFFFFF" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6" 
        android:background="#FF0000">

          <fragment
        android:id="@+id/First2Appointments"
        android:name="kb50.appointment.ProfielListFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            />

        <!-- <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:id="@+id/simple_list_item_1">

        </ListView> 

    </LinearLayout>

</LinearLayout>

profiellistfragment_layout.xml

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

     <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_weight="0.4" 
        android:layout_height="wrap_content"
        android:textSize="15sp" 
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"/>


    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="0.6" 
        android:layout_height="100dp"
        android:orientation="vertical" 
        android:padding="10dp"

        >

        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp" />

    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。在Vidar Wahlberg的回答中:

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

我父母是NULL