java.lang.IllegalArgumentException:找不到片段片段的id(someid)(id /)视图

时间:2014-03-19 09:28:21

标签: android android-fragments illegalargumentexception

在我的应用中,我有一些片段。在我使用以下代码的活动中,我在Fragment

的片段组中加载了Fragment
FragmentManager fm;
FragmentTransaction ft = fm.beginTransaction();
f3 = new CustomerTransactionFragment();
ft.add(R.id.fragmentgroup,f3,"C");
ft.commit();
<{1>};

中的

CustomerTransactionFragment()

此处public class CustomerTransactionFragment extends SherlockFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.customertransaction, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Fragment fragment = null; fragment = new CustomerSearchFragment(); if (fragment != null) { FragmentManager fragmentManager = getChildFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.customertransaction_form_fragmentgroup, fragment).commit(); } } } _ form_fragmentgroup在customertransaction中为LinearLayout,用于加载customertransaction.xml之类的子Fragment

因为长度来自CustomerSearchFragment我只是在下面表示基本代码,

CustomerSearchFragment

我没有包含设置适配器的代码。因为加载适配器没有问题。它完美地显示了项目,并且在项目点击时也会选择当前项目。当我执行项目点击时,当我尝试用 ListView lv; ListAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.customerlist, container, false); @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); lv = (ListView) view .findViewById(R.id.ccustomerlist_form_ListViewcustomers); adapter = new SimpleAdapter(getActivity(), dataList, R.layout.singlecustomerselect, new String[] { TAG_NAME, TAG_ID, TAG_PHONE }, new int[] { R.id.sincusname, R.id.sincusid, R.id.sincusgsm }); lv.setAdapter(adapter); 中的新Fragment替换当前customertransaction_form_fragmentgroup时出现此问题。

lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem

                Toast.makeText(
                        getActivity(),
                        "NAME:" + selname + "\nCODE:" + selcode
                                + "\nPHONE:" + selphno, Toast.LENGTH_LONG)
                        .show();
                  Fragment fragment = null;
                  fragment = new CustomerDetailDisplayFragment();
                   if (fragment != null) {
                       FragmentManager fragmentManager = getChildFragmentManager();

                        fragmentManager.beginTransaction()
                                .replace(R.id.customertransaction_form_fragmentgroup, fragment).commit();
                   } 


            }
        });

}

日志猫位于下方

03-19 09:05:08.156: W/dalvikvm(1761): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-19 09:05:08.176: E/AndroidRuntime(1761): FATAL EXCEPTION: main
03-19 09:05:08.176: E/AndroidRuntime(1761): java.lang.IllegalArgumentException: No view found for id 0x7f040054 (com.noufal.posfragment:id/customertransaction_form_fragmentgroup) for fragment CustomerDetailDisplayFragment{405bf608 #0 id=0x7f040054}
03-19 09:05:08.176: E/AndroidRuntime(1761):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at android.os.Handler.handleCallback(Handler.java:587)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at android.os.Looper.loop(Looper.java:123)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at java.lang.reflect.Method.invoke(Method.java:507)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 09:05:08.176: E/AndroidRuntime(1761):     at dalvik.system.NativeStart.main(Native Method

请提前帮助找到解决方案。谢谢。

1 个答案:

答案 0 :(得分:0)

确保CustomerDetailDisplayFragment是否有View。例如,您是否为FragmentCustomerDetailDisplayFragment)分配了布局。如果未在下一个View中创建Fragment,则会抛出异常。