我正在嘗試構建一個應用程序,通過與其用戶的交互來交換其片段。 在Activity的XML中定義了一個Fragment,而LinearLayout應該包含另一個Fragment。Android 3 - 以編程方式添加片段:非法狀態異常
我的主要活動:
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: [email protected]/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
+1,這對我來說確實很奇怪。我現在習慣了,所以我甚至沒有想到,但我仍然想知道爲什麼它不起作用。 – Guillaume
謝謝 - 這幫了我很大的力量 – caiuspb
在我看到你的答案之前,我甚至可以聽到我的感謝之詞,甚至在我嘗試過之前。謝謝!我花了幾個小時試圖調試什麼導致OP得到相同的異常! –