android-以编程方式将视图添加到Fragment onCreateView方法

android-以编程方式将视图添加到Fragment onCreateView方法,第1张

概述我正在使用片段为API15构建一个Android应用.布局为:>activity_page_detail.xml>activity_page_list.xml>activity_page_twopane.xml>fragment_page_detail.xmlJava文件是:>pageDetailActivity.java>pageDetailFragment.java>pageListActivity.java>pageListFr

我正在使用片段为API15构建一个Android应用.

布局为:

> activity_page_detail.xml
> activity_page_List.xml
> activity_page_twopane.xml
> fragment_page_detail.xml

Java文件是:

> pageDetailActivity.java
> pageDetailFragment.java
> PagelistActivity.java
> PagelistFragment.java
> DummyContent.java

在activity_page_detail.xml中:

<FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:ID="@+ID/page_detail_container"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"tools:context=".pageDetailActivity"tools:ignore="MergeRootFrame"/>

在activity_page_List.xml中:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"tools:ignore="contentDescription"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical" androID:gravity="center"><ImageVIEw        androID:ID="@+ID/logoimage"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:src="@drawable/logo"         androID:layout_margintop="20dp"        androID:layout_marginBottom="30dp"/><fragment    androID:ID="@+ID/page_List"    androID:name="com.application.trigger.PagelistFragment"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:layout_marginleft="16dp"    androID:layout_marginRight="16dp"    tools:context=".PagelistActivity"    tools:layout="@androID:layout/List_content" /></linearLayout>

并在activity_page_twopane.xml中:

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:layout_marginleft="16dp"androID:layout_marginRight="16dp"androID:baselineAligned="false"androID:divIDer="?androID:attr/divIDerHorizontal"androID:orIEntation="horizontal"androID:showdivIDers="mIDdle"tools:context=".PagelistActivity" ><!--This layout is a two-pane layout for the pagesmaster/detail flow. See res/values-large/refs.xml andres/values-sw600dp/refs.xml for an example of layout aliasesthat replace the single-pane version of the layout withthis two-pane version.For more on layout aliases, see:http://developer.androID.com/training/multiscreen/screensizes.HTML#TaskUseAliasFilters--><fragment    androID:ID="@+ID/page_List"    androID:name="com.application.trigger.PagelistFragment"    androID:layout_wIDth="0dp"    androID:layout_height="match_parent"    androID:layout_weight="1"    tools:layout="@androID:layout/List_content" /><FrameLayout    androID:ID="@+ID/page_detail_container"    androID:layout_wIDth="0dp"    androID:layout_height="match_parent"    androID:layout_weight="3" /></linearLayout>

并在fragment_page_detail.xml中:

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"tools:context=".pageDetailFragment"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical" tools:ignore="contentDescription"androID:background="#000"><TextVIEw    androID:ID="@+ID/page_detail"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:padding="16dp"    androID:textcolor="#fafafa"    androID:textIsSelectable="true"    androID:textAppearance="@androID:attr/textAppearanceListItemSmall"    /><linearLayout   androID:ID="@+ID/linear_vertical"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical"   androID:background="#7f7f7f"   /></linearLayout>

Java源代码为:

package com.application.trigger;import androID.content.Intent;import androID.os.Bundle;import androID.support.v4.app.FragmentActivity;/** * An activity representing a List of pages. This activity * has different presentations for handset and tablet-size devices. On * handsets, the activity presents a List of items, which when touched, * lead to a {@link pageDetailActivity} representing * item details. On tablets, the activity presents the List of items and * item details sIDe-by-sIDe using two vertical panes. * <p> * The activity makes heavy use of fragments. The List of items is a * {@link PagelistFragment} and the item details * (if present) is a {@link pageDetailFragment}. * <p> * This activity also implements the required * {@link PagelistFragment.Callbacks} interface * to Listen for item selections. */public class PagelistActivity extends FragmentActivity        implements PagelistFragment.Callbacks {/** * Whether or not the activity is in two-pane mode, i.e. running on a tablet * device. */private boolean mTwoPane;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_page_List);    if (findVIEwByID(R.ID.page_detail_container) != null) {        // The detail container vIEw will be present only in the        // large-screen layouts (res/values-large and        // res/values-sw600dp). If this vIEw is present, then the        // activity should be in two-pane mode.        mTwoPane = true;        // In two-pane mode, List items should be given the        // 'activated' state when touched.        ((PagelistFragment) getSupportFragmentManager()                .findFragmentByID(R.ID.page_List))                .setActivateOnItemClick(true);    }    // Todo: If exposing deep links into your app, handle intents here.}/** * Callback method from {@link PagelistFragment.Callbacks} * indicating that the item with the given ID was selected. */@OverrIDepublic voID onItemSelected(String ID) {    if (mTwoPane) {        // In two-pane mode, show the detail vIEw in this activity by        // adding or replacing the detail fragment using a        // fragment transaction.        Bundle arguments = new Bundle();        arguments.putString(pageDetailFragment.ARG_ITEM_ID, ID);        pageDetailFragment fragment = new pageDetailFragment();        fragment.setArguments(arguments);        getSupportFragmentManager().beginTransaction()                .replace(R.ID.page_detail_container, fragment)                .commit();    } else {        // In single-pane mode, simply start the detail activity        // for the selected item ID.        Intent detailintent = new Intent(this, pageDetailActivity.class);        detailintent.putExtra(pageDetailFragment.ARG_ITEM_ID, ID);        startActivity(detailintent);    }}}

package com.application.trigger;import androID.app.Activity;import androID.os.Bundle;import androID.support.v4.app.ListFragment;import androID.vIEw.VIEw;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import com.application.trigger.dummy.DummyContent;/** * A List fragment representing a List of pages. This fragment * also supports tablet devices by allowing List items to be given an * 'activated' state upon selection. This helps indicate which item is * currently being vIEwed in a {@link pageDetailFragment}. * <p> * ActivitIEs containing this fragment MUST implement the {@link Callbacks} * interface. */public class PagelistFragment extends ListFragment {/** * The serialization (saved instance state) Bundle key representing the * activated item position. Only used on tablets. */private static final String STATE_ACTIVATED_position = "activated_position";/** * The fragment's current callback object, which is notifIEd of List item * clicks. */private Callbacks mCallbacks = sDummyCallbacks;/** * The current activated item position. Only used on tablets. */private int mActivatedposition = ListVIEw.INVALID_position;/** * A callback interface that all activitIEs containing this fragment must * implement. This mechanism allows activitIEs to be notifIEd of item * selections. */public interface Callbacks {    /**     * Callback for when an item has been selected.     */    public voID onItemSelected(String ID);}/** * A dummy implementation of the {@link Callbacks} interface that does * nothing. Used only when this fragment is not attached to an activity. */private static Callbacks sDummyCallbacks = new Callbacks() {    @OverrIDe    public voID onItemSelected(String ID) {    }};/** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orIEntation changes). */public PagelistFragment() {}@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    // Todo: replace with a real List adapter.    setlistadapter(new ArrayAdapter<DummyContent.DummyItem>(            getActivity(),            androID.R.layout.simple_List_item_activated_1,            androID.R.ID.text1,            DummyContent.ITEMS));}@OverrIDepublic voID onVIEwCreated(VIEw vIEw, Bundle savedInstanceState) {    super.onVIEwCreated(vIEw, savedInstanceState);    // Restore the prevIoUsly serialized activated item position.    if (savedInstanceState != null            && savedInstanceState.containsKey(STATE_ACTIVATED_position)) {        setActivatedposition(savedInstanceState.getInt(STATE_ACTIVATED_position));    }}@OverrIDepublic voID onAttach(Activity activity) {    super.onAttach(activity);    // ActivitIEs containing this fragment must implement its callbacks.    if (!(activity instanceof Callbacks)) {        throw new IllegalStateException("Activity must implement fragment's callbacks.");    }    mCallbacks = (Callbacks) activity;}@OverrIDepublic voID onDetach() {    super.onDetach();    // reset the active callbacks interface to the dummy implementation.    mCallbacks = sDummyCallbacks;}@OverrIDepublic voID onListItemClick(ListVIEw ListVIEw, VIEw vIEw, int position, long ID) {    super.onListItemClick(ListVIEw, vIEw, position, ID);    // Notify the active callbacks interface (the activity, if the    // fragment is attached to one) that an item has been selected.    mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).ID);}@OverrIDepublic voID onSaveInstanceState(Bundle outState) {    super.onSaveInstanceState(outState);    if (mActivatedposition != ListVIEw.INVALID_position) {        // Serialize and persist the activated item position.        outState.putInt(STATE_ACTIVATED_position, mActivatedposition);    }}/** * Turns on activate-on-click mode. When this mode is on, List items will be * given the 'activated' state when touched. */public voID setActivateOnItemClick(boolean activateOnItemClick) {    // When setting CHOICE_MODE_SINGLE, ListVIEw will automatically    // give items the 'activated' state when touched.    getListVIEw().setChoiceMode(activateOnItemClick            ? ListVIEw.CHOICE_MODE_SINGLE            : ListVIEw.CHOICE_MODE_NONE);}private voID setActivatedposition(int position) {    if (position == ListVIEw.INVALID_position) {        getListVIEw().setItemChecked(mActivatedposition, false);    } else {        getListVIEw().setItemChecked(position, true);    }    mActivatedposition = position;}}

和:

    package com.application.trigger;    import androID.content.Intent;    import androID.os.Bundle;    import androID.support.v4.app.FragmentActivity;    import androID.support.v4.app.NavUtils;    import androID.vIEw.MenuItem;    /**     * An activity representing a single page detail screen. This     * activity is only used on handset devices. On tablet-size devices,     * item details are presented sIDe-by-sIDe with a List of items     * in a {@link PagelistActivity}.     * <p>     * This activity is mostly just a 'shell' activity containing nothing     * more than a {@link pageDetailFragment}.     */    public class pageDetailActivity extends FragmentActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_page_detail);        // Show the Up button in the action bar.        getActionbar().setdisplayHomeAsUpEnabled(true);        // savedInstanceState is non-null when there is fragment state        // saved from prevIoUs configurations of this activity        // (e.g. when rotating the screen from portrait to landscape).        // In this case, the fragment will automatically be re-added        // to its container so we don't need to manually add it.        // For more information, see the Fragments API guIDe at:        //        // http://developer.androID.com/guIDe/components/fragments.HTML        //        if (savedInstanceState == null) {            // Create the detail fragment and add it to the activity            // using a fragment transaction.            Bundle arguments = new Bundle();            arguments.putString(pageDetailFragment.ARG_ITEM_ID,                    getIntent().getStringExtra(pageDetailFragment.ARG_ITEM_ID));            pageDetailFragment fragment = new pageDetailFragment();            fragment.setArguments(arguments);            getSupportFragmentManager().beginTransaction()                    .add(R.ID.page_detail_container, fragment)                    .commit();        }    }    @OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        switch (item.getItemID()) {            case androID.R.ID.home:                // This ID represents the Home or Up button. In the case of this                // activity, the Up button is shown. Use NavUtils to allow users                // to navigate up one level in the application structure. For                // more details, see the Navigation pattern on AndroID Design:                //                // http://developer.androID.com/design/patterns/navigation.HTML#up-vs-back                //                NavUtils.navigateUpTo(this, new Intent(this, PagelistActivity.class));                return true;            }            return super.onoptionsItemSelected(item);        }    }

和:

package com.application.trigger.dummy;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Helper class for provIDing sample content for user interfaces created by * AndroID template wizards. * <p> * Todo: Replace all uses of this class before publishing your app. */public class DummyContent {/** * An array of sample (dummy) items. */public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();/** * A map of sample (dummy) items, by ID. */public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();static {    // Add 3 sample items.    addItem(new DummyItem("1", "ListItem-1"));    addItem(new DummyItem("2", "ListItem-2"));    addItem(new DummyItem("3", "ListItem-3"));    addItem(new DummyItem("4", "ListItem-4"));    addItem(new DummyItem("5", "ListItem-5"));}private static voID addItem(DummyItem item) {    ITEMS.add(item);    ITEM_MAP.put(item.ID, item);}/** * A dummy item representing a pIEce of content. */public static class DummyItem {    public String ID;    public String content;    public DummyItem(String ID, String content) {        this.ID = ID;        this.content = content;    }    @OverrIDe    public String toString() {        return content;    }}}

最后,与我的问题相关的代码文件是:

        package com.application.trigger;        import java.util.ArrayList;        import androID.content.ContentValues;        import androID.content.Intent;        import androID.os.Bundle;        import androID.provIDer.ContactsContract;        import androID.provIDer.ContactsContract.CommonDataKinds.Email;        import androID.provIDer.ContactsContract.CommonDataKinds.Organization;        import androID.provIDer.ContactsContract.CommonDataKinds.Phone;        import androID.provIDer.ContactsContract.CommonDataKinds.Website;        import androID.provIDer.ContactsContract.Data;        import androID.provIDer.ContactsContract.Intents.Insert;        import androID.support.v4.app.Fragment;        import androID.vIEw.LayoutInflater;        import androID.vIEw.VIEw;        import androID.vIEw.VIEwGroup;        import androID.vIEw.VIEwGroup.LayoutParams;        import androID.Widget.ImageVIEw;        import androID.Widget.linearLayout;        import androID.Widget.TextVIEw;        import com.application.trigger.dummy.DummyContent;        /**         * A fragment representing a single page detail screen.         * This fragment is either contained in a {@link PagelistActivity}         * in two-pane mode (on tablets) or a {@link pageDetailActivity}         * on handsets.         */        public class pageDetailFragment extends Fragment {            /**             * The fragment argument representing the item ID that this fragment             * represents.             */            public static final String ARG_ITEM_ID = "item_ID";            /**             * The dummy content this fragment is presenting.             */            private DummyContent.DummyItem mItem;            /**             * Mandatory empty constructor for the fragment manager to instantiate the             * fragment (e.g. upon screen orIEntation changes).             */            public pageDetailFragment() {            }        @OverrIDe        public voID onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            if (getArguments().containsKey(ARG_ITEM_ID)) {                // Load the dummy content specifIEd by the fragment                // arguments. In a real-world scenario, use a Loader                // to load content from a content provIDer.                mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));                getActivity().setTitle(mItem.content);            }        }        @OverrIDe        public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,                Bundle savedInstanceState) {            VIEw rootVIEw = inflater.inflate(R.layout.fragment_page_detail, container, false);            // show each product specification            //passing data to each page            if (mItem != null) {                switch (Integer.parseInt(mItem.ID)) {                case 1:                    ((TextVIEw) rootVIEw.findVIEwByID(R.ID.page_detail))                    .setText("show text1");                    final linearLayout row1 = new linearLayout(getActivity());                    row1.setorIEntation(linearLayout.HORIZONTAL);                    row1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));                    row1.setVisibility(VIEw.VISIBLE);                    final TextVIEw text1 = new TextVIEw(getActivity());                    text1.setText("some sample text");                    text1.setVisibility(VIEw.VISIBLE);                    final ImageVIEw image1 = new ImageVIEw(getActivity());                    image1.setVisibility(VIEw.VISIBLE);                    image1.setimageResource(R.drawable.logo);                    row1.addVIEw(text1);                    row1.addVIEw(image1);                    //linearLayout vertical = (linearLayout) rootVIEw.findVIEwByID(R.ID.linear_vertical);                    //vertical.addVIEw(row1);                    ((VIEwGroup)rootVIEw).addVIEw(row1);                    break;                case 2:                    ((TextVIEw) rootVIEw.findVIEwByID(R.ID.page_detail))                    .setText("show text2");                    break;                case 3:                    ((TextVIEw) rootVIEw.findVIEwByID(R.ID.page_detail))                    .setText("show text3");                    break;                case 4:                    Intent contact = new Intent(Intent.ACTION_INSERT);                    contact.setType(ContactsContract.Contacts.CONTENT_TYPE);                    ArrayList<ContentValues> data = new ArrayList<ContentValues>();                    ContentValues company = new ContentValues();                    company.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);                    company.put(Organization.COMPANY, "company name");                    data.add(company);                    ContentValues mobile = new ContentValues();                    mobile.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);                    mobile.put(Phone.NUMBER, "9898989898");                    data.add(mobile);                    ContentValues phone = new ContentValues();                    phone.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);                    phone.put(Phone.TYPE, Phone.TYPE_WORK);                    phone.put(Phone.NUMBER, "9898989898");                    data.add(phone);                    ContentValues email = new ContentValues();                    email.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);                    email.put(Email.TYPE, Email.TYPE_WORK);                    email.put(Email.ADDRESS, "[email protected]");                    data.add(email);                    ContentValues website = new ContentValues();                    website.put(Data.MIMETYPE, Website.CONTENT_ITEM_TYPE);                    website.put(Website.URL, "http://company.com");                         data.add(website);                    contact.putParcelableArrayListExtra(Insert.DATA, data);                    int request_Code = 100;                    startActivityForResult(contact, request_Code);                    break;                case 5:                    ((TextVIEw) rootVIEw.findVIEwByID(R.ID.page_detail))                    .setText("show text5");                    break;                }            }            return rootVIEw;        }    }

问题是如何在onCreateVIEw方法中以编程方式为最后一个Java代码中的每个片段详细信息添加视图?在案例1中提到的功能处的“我的代码”退出了程序,无法正常工作.

这是相关的堆栈跟踪:

08-04 06:19:35.623: DEBUG/dalvikvm(1153): GC_CONCURRENT freed 84K, 3% free 9379K/9607K, paused 4ms+4ms08-04 06:19:39.283: WARN/WindowManager(89): Failure taking screenshot for (180x300) to layer 2101508-04 06:19:43.063: INFO/ActivityManager(89): START {cmp=com.application.trigger/.pageDetailActivity (has extras)} from pID 115308-04 06:19:43.074: WARN/WindowManager(89): Failure taking screenshot for (180x300) to layer 2101008-04 06:19:43.233: DEBUG/AndroIDRuntime(1153): Shutting down VM08-04 06:19:43.233: WARN/dalvikvm(1153): threadID=1: thread exiting with uncaught exception (group=0x2ba041f8)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153): FATAL EXCEPTION: main08-04 06:19:43.265: ERROR/AndroIDRuntime(1153): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.trigger/com.application.trigger.pageDetailActivity}: java.lang.IllegalStateException: The specifIEd child already has a parent. You must call removeVIEw() on the child's parent first.08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1956)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.app.ActivityThread.access0(ActivityThread.java:123)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.os.Handler.dispatchMessage(Handler.java:99)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.os.Looper.loop(Looper.java:137)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.app.ActivityThread.main(ActivityThread.java:4424)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at java.lang.reflect.Method.invokeNative(Native Method)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at java.lang.reflect.Method.invoke(Method.java:511)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at dalvik.system.NativeStart.main(Native Method)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153): Caused by: java.lang.IllegalStateException: The specifIEd child already has a parent. You must call removeVIEw() on the child's parent first.08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.vIEw.VIEwGroup.addVIEwInner(VIEwGroup.java:3337)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.vIEw.VIEwGroup.addVIEw(VIEwGroup.java:3208)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.vIEw.VIEwGroup.addVIEw(VIEwGroup.java:3165)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.vIEw.VIEwGroup.addVIEw(VIEwGroup.java:3145)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at com.application.trigger.pageDetailFragment.onCreateVIEw(pageDetailFragment.java:98)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.support.v4.app.Fragment.performCreateVIEw(Fragment.java:1478)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:927)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1104)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.app.Activity.performStart(Activity.java:4475)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1929)08-04 06:19:43.265: ERROR/AndroIDRuntime(1153):     ... 11 more08-04 06:19:43.293: WARN/ActivityManager(89):   Force finishing activity com.application.trigger/.pageDetailActivity08-04 06:19:43.323: WARN/ActivityManager(89):   Force finishing activity com.application.trigger/.PagelistActivity08-04 06:19:43.633: INFO/Process(89): Sending signal. PID: 1153 SIG: 3

解决方法:

堆栈跟踪明确表明问题是您试图将已附加到VIEwGroup(其父级)的VIEw添加到另一个VIEwGroup.

一个视图不能有两个不同的父级,因此会出现错误.

您可以先将视图从其父视图中移除,然后再将其添加到新的父视图中,也可以简单地多次膨胀相同的XML.如果您将一种XML膨胀更多,那么您将创建新的VIEw对象,并且可以将它们附加到其他父对象.

总结

以上是内存溢出为你收集整理的android-以编程方式将视图添加到Fragment onCreateView方法全部内容,希望文章能够帮你解决android-以编程方式将视图添加到Fragment onCreateView方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1076278.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存