从另一个调用片段

时间:2012-07-18 15:19:40

标签: android android-fragments

我有Mainactivity扩展FragmentActivity,我在布局中使用

加载片段
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();
患者搜索中的

我使用

调用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

2 个答案:

答案 0 :(得分:2)

只需添加片段,您只需要Fragment的实例,

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Search fragment = new  Search();
fragmentTransaction.add(R.id.mainLayout,fragment , "MY_FRAG");
fragmentTransaction.commit();

答案 1 :(得分:1)

我不确定我是否正确地告诉你,但你无法通过意图开始新的片段。如果AppointementSearch.class是片段,则无法使用此行:

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

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