Android - java.lang.IllegalArgumentException:找不到用于片段的id xxxxx(android:id / content)的视图

时间:2015-03-05 15:58:49

标签: java android xml android-fragments

我想切换到另一个片段,但是从昨天开始我收到了此错误消息。顺便说一句,这个片段在TabHost内。

03-05 23:51:15.247: E/AndroidRuntime(8780):
java.lang.IllegalArgumentException: 
No view found for id 0x7f0d00ac (com.xxxxx:id/container_framelayout) 
for fragment SecondFragment{41a44310 #0 id=0x7f0d00ac}

FirstFragment.java

public class FirstFragment extends Fragment {
    private ListView listView;

    public FirstFragment() {
        // TODO Auto-generated constructor stub

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.activity_first_fragment,
                null);

        listView = (ListView) v.findViewById(R.id.list);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                getChildFragmentManager().beginTransaction().add(R.id.container_framelayout, new SecondFragment()).commit();
                getChildFragmentManager().executePendingTransactions();
            }


        });
    }
}

我已经在container_framelayout.xml中定义了id

<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container_framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </FrameLayout>

编辑:我把FirstFragment放在哪里

ParentFragment.java

public class ParentFragment extends Fragment {

    private static final String TAB_1_TAG = "first";
    private FragmentTabHost mTabHost;

    public ParentFragment() {
        // TODO Auto-generated constructor stub

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.top_tabs);

        Bundle b = new Bundle();
        b.putString("key", "First");
        mTabHost.addTab(mTabHost.newTabSpec(TAB_1_TAG).setIndicator("First"),
            FirstFragment.class, b);

        return mTabHost;
    }

}

请指教。谢谢。

2 个答案:

答案 0 :(得分:1)

修改:

View v = LayoutInflater.from(getActivity()).inflate(R.layout.activity_first_fragment,
                null);

进入这个:

View v = inflater.inflate(R.layout.activity_first_fragment, container
            false);

答案 1 :(得分:1)

我已经找到了答案。在onItemClick函数中,我需要像这样,

@Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                SecondFragment secondFragment = new SecondFragment();
                FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.add(R.id.first_fragment, secondFragment);
                fragmentTransaction.commit();
            }