我正在尝试构建一个App,它通过与用户的交互来交换它的片段。 在Activity的XML和LinearLayout中定义了一个Fragment,它应该包含另一个Fragment。
我的主要活动:
public class DashActivity extends Activity {
private static final String TAG = "DashActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
//-- Instantiate Fragmentstuff
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ActivitystreamFragment asf = new ActivitystreamFragment();
fragmentTransaction.add(R.id.rightfrag, asf);
fragmentTransaction.commit();
Log.i(TAG, "RightView has been committed");
}
其XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/fragment_dashboard"
android:layout_width="5dp"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_weight="0.97"
android:name="de.upb.cs.ginkgo.tablet.ui.fragments.DashboardFragment" >
<!-- Preview: layout=@layout/fragment_dashboard -->
</fragment>
<LinearLayout
android:id="@+id/rightfrag"
android:layout_width="450dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp" >
</LinearLayout>
</LinearLayout>
我的片段:
public class ActivitystreamFragment extends ListFragment {
private static final String TAG = "ActivitystreamFragment";
ArrayList<Status> states;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG,"Begin: Inflate");
View root = inflater.inflate(R.layout.fragment_activitystream, container);
Log.i(TAG,"Finished: Inflate");
if(root == null)
Log.e(TAG, "omg Inflate == null");
return root;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
states = new ArrayList<Status>();
// THE FOLLOWING HAS TO BE DELETED LATER!!
Profile p1 = new Profile("1", null, "Max Muster",
null, null, null, null, null, null, null);
Status status1 = new Status(p1,"Hello world", "12.02.2012", null);
states.add(status1);
// --- DELTE ----------------------------
ActivitystreamAdapter asa = new ActivitystreamAdapter(getActivity(), R.layout.list_item_event, states);
setListAdapter(asa);
Log.i(TAG, "onCreate is done");
}
}
片段XML:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:background="@drawable/back_rightfrag" >
</ListView>
Logcat的输出:
01-25 14:14:29.788: I/Process(781): Sending signal. PID: 781 SIG: 9
01-25 14:17:32.387: I/DashActivity(814): RightView has been committed
01-25 14:17:32.387: I/ActivitystreamFragment(814): onCreate is done
01-25 14:17:32.397: I/ActivitystreamFragment(814): Begin: Inflate
01-25 14:17:32.467: D/dalvikvm(814): GC_FOR_ALLOC freed 86K, 3% free 6509K/6663K, paused 59ms
01-25 14:17:32.477: I/dalvikvm-heap(814): Grow heap (frag case) to 6.902MB for 513744-byte allocation
01-25 14:17:32.597: D/dalvikvm(814): GC_CONCURRENT freed 3K, 3% free 7007K/7175K, paused 4ms+4ms
01-25 14:17:32.657: I/ActivitystreamFragment(814): Finished: Inflate
01-25 14:17:32.657: D/AndroidRuntime(814): Shutting down VM
01-25 14:17:32.657: W/dalvikvm(814): threadid=1: thread exiting with uncaught exception (group=0x40014760)
01-25 14:17:32.677: E/AndroidRuntime(814): FATAL EXCEPTION: main
01-25 14:17:32.677: E/AndroidRuntime(814): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.upb.cs.ginkgo.tablet/de.upb.cs.ginkgo.tablet.ui.DashActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.ActivityThread.access$500(ActivityThread.java:122)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.os.Looper.loop(Looper.java:132)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.ActivityThread.main(ActivityThread.java:4123)
01-25 14:17:32.677: E/AndroidRuntime(814): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 14:17:32.677: E/AndroidRuntime(814): at java.lang.reflect.Method.invoke(Method.java:491)
01-25 14:17:32.677: E/AndroidRuntime(814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 14:17:32.677: E/AndroidRuntime(814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 14:17:32.677: E/AndroidRuntime(814): at dalvik.system.NativeStart.main(Native Method)
01-25 14:17:32.677: E/AndroidRuntime(814): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-25 14:17:32.677: E/AndroidRuntime(814): at android.view.ViewGroup.addViewInner(ViewGroup.java:3011)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.view.ViewGroup.addView(ViewGroup.java:2900)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.view.ViewGroup.addView(ViewGroup.java:2857)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.view.ViewGroup.addView(ViewGroup.java:2837)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:787)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.BackStackRecord.run(BackStackRecord.java:638)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1309)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.Activity.performStart(Activity.java:4406)
01-25 14:17:32.677: E/AndroidRuntime(814): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1788)
01-25 14:17:32.677: E/AndroidRuntime(814): ... 11 more
答案 0 :(得分:13)
更改行
View root = inflater.inflate(R.layout.fragment_activitystream, container);
到
View root = inflater.inflate(R.layout.fragment_activitystream, null);
甚至更好:
View root = inflater.inflate(R.layout.fragment_activitystream, container, false);
它应该有效。不要问我为什么将片段附加到你希望它进入的容器中是不行的,但它没有...
答案 1 :(得分:0)
您无法动态删除/替换从xml添加的Fragments
。为了达到您想要的效果,您需要替换xml中的<fragment>
标记,例如FrameLayout
,并且只需要add
和remove
个片段FrameLayout
。