0

我有扩展 FragmentActivity 的 Mainactivity ,我正在使用布局中加载片段

LinearLayout formLayout = (LinearLayout)findViewById(R.id.mainLayout);
        formLayout.removeAllViews();


          FragmentTransaction fragmentTransaction =      fragmentManager.beginTransaction();


         Search fragment = new  Search(maincontrolActivity.this);

          fragmentTransaction.add(R.id.mainLayout,fragment , "MY_FRAG");
          fragmentTransaction.commit();

在 PatientSearch 我通过使用调用 apoointementsearch (另一个片段)

startActivity(new Intent(Search.this.getActivity(), AppointementSearch.class).putExtra("Position",0));

but it crashs giving this log ,I am sure that I put them in the AndroidManifest

07-18 17:13:15.780: E/AndroidRuntime(3579): FATAL EXCEPTION: main
07-18 17:13:15.780: E/AndroidRuntime(3579): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.appnetics/com.appnetics.AppointementSearch}: java.lang.InstantiationException: com.appnetics.AppointementSearch
07-18 17:13:15.780: E/AndroidRuntime(3579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at android.os.Looper.loop(Looper.java:132)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at android.app.ActivityThread.main(ActivityThread.java:4123)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at java.lang.reflect.Method.invoke(Method.java:491)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at dalvik.system.NativeStart.main(Native Method)
07-18 17:13:15.780: E/AndroidRuntime(3579): Caused by: java.lang.InstantiationException: com.appnetics.AppointementSearch
07-18 17:13:15.780: E/AndroidRuntime(3579):     at java.lang.Class.newInstanceImpl(Native Method)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at java.lang.Class.newInstance(Class.java:1301)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at android.app.Instrumentation.newActivity(Instrumentation.java:1025)
07-18 17:13:15.780: E/AndroidRuntime(3579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1730)
07-18 17:13:15.780: E/AndroidRuntime(3579):     ... 11 more
4

2 回答 2

2

只需添加一个片段,您只需要一个Fragment,

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Search fragment = new  Search();
fragmentTransaction.add(R.id.mainLayout,fragment , "MY_FRAG");
fragmentTransaction.commit();
于 2012-07-19T06:51:43.537 回答
1

我不确定我是否正确理解了你,但你不能有意地开始新的片段。如果AppointementSearch.class是片段,则不能使用此行:

startActivity(new Intent(Search.this.getActivity(), AppointementSearch.class).putExtra("Position",0));

显示新片段的使用FragmentManager或启动包含该片段的新活动。

于 2012-07-19T06:46:20.797 回答