添加多个片段编程

添加多个片段编程

本文介绍了添加多个片段编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关项目的短信息:


  • minSDK = 8

  • 进口android.support.v4

类code的部分:

public class DetailsActivity extends FragmentActivity implements ToplineTrigger {

static Context mContext;

private static FragmentManager fm;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details);
    if (mContext == null)
        mContext = Pleazzme.getAppContext();

    DetailsTitleFragment title = new DetailsTitleFragment();
    DocumentDescriptionWeb description = new DocumentDescriptionWeb();
    getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.detailstitlefragment_wrap, title, "DetailsTitle")
            .add(R.id.documentdescriptionweb_wrap, description, "WebDescription")
            .commit();

    ((Topline) fm.findFragmentByTag("Topline")).setDetailsMode();
};

XML视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/details_contentwrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    class="com.mtechnologia.pleazzme.Topline"
    android:tag="Topline" />

<FrameLayout
    android:id="@+id/detailstitlefragment_wrap"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</FrameLayout>

<FrameLayout
    android:id="@+id/documentdescriptionweb_wrap"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</FrameLayout>
</LinearLayout>

类声明:

public class DetailsTitleFragment extends ListFragment{...}

public class DocumentDescriptionWeb extends Fragment {

WebView mWebView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.simplewebview, container);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mWebView = (WebView) view.findViewById(R.id.simplewebview);
    mWebView.getSettings().setJavaScriptEnabled(false);
    mWebView.getSettings().setSupportZoom(false);

    String data = "asdasdasd";
    mWebView.loadData(data, "text/html", "utf-8");
}

}

code运行后,我得到错误:

after code runs i get error:

08-15 19:11:08.003: E/AndroidRuntime(24708): FATAL EXCEPTION: main
08-15 19:11:08.003: E/AndroidRuntime(24708): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mtechnologia.pleazzme/com.mtechnologia.pleazzme.DetailsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.os.Looper.loop(Looper.java:137)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.app.ActivityThread.main(ActivityThread.java:4511)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at java.lang.reflect.Method.invokeNative(Native Method)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at java.lang.reflect.Method.invoke(Method.java:511)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at dalvik.system.NativeStart.main(Native Method)
08-15 19:11:08.003: E/AndroidRuntime(24708): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3342)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.view.ViewGroup.addView(ViewGroup.java:3213)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.view.ViewGroup.addView(ViewGroup.java:3170)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.view.ViewGroup.addView(ViewGroup.java:3150)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:875)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1136)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.app.Activity.performStart(Activity.java:4480)
08-15 19:11:08.003: E/AndroidRuntime(24708):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940)
08-15 19:11:08.003: E/AndroidRuntime(24708):    ... 11 more

有人可以告诉我,我做错了什么?结果
我知道,我可以在XML声明两个片段,但我需要编程添加此片段,以取代他们,当我需要这个。
在此先感谢您的帮助。

can someone tell me, what i doing wrong?
i know, that i can declare both fragments in XML, but i need add this fragments programmatically, to replace them when i need this.Thanks in advance for your help

推荐答案

您需要修改

inflater.inflate(R.layout.simplewebview, container);

inflater.inflate(R.layout.simplewebview, container, false);

设置该属性为将确保该视图是从在从视图通过父容器detatched。在查看这是一个从 onCreateView()不能连接到父,因为它会被添加到容器返回不久,由系统本身了。

Setting that attribute to false will ensure that the view is detatched from the parent container that's passed in from the view. The View that's returned from onCreateView() can't be attached to a parent since it will be added to the container shortly after by the system itself.

这篇关于添加多个片段编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:30