在我的应用程序中,我有一些片段。从我使用以下代码的活动中,我将Fragment
加载到片段组中以供Fragment
FragmentManager fm;
FragmentTransaction ft = fm.beginTransaction();
f3 = new CustomerTransactionFragment();
ft.add(R.id.fragmentgroup,f3,"C");
ft.commit();
在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();
}
}
}
这里customertransaction
_form_fragmentgroup 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);
我没有包含用于设置适配器的代码,因为加载适配器没有问题。它完美地显示项目,并且在项目单击时也会选择当前项目。当我尝试在customertransaction_form_fragmentgroup
中用新Fragment
替换当前时,当执行项目单击时,出现问题。
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
(
请帮助找到解决方案。提前感谢..
确保CustomerDetailDisplayFragment
是否有View
.例如,是否为该Fragment
分配布局 ( CustomerDetailDisplayFragment
(。如果在 next 中未创建View
Fragment
将引发异常。