将一个对象从我的 activity 传递到我的片段

Passing a object from my activity to my fragment

一段时间以来,我一直在尝试解决这个问题,现在我尝试按照此处的一些示例进行操作,但我似乎遗漏了一些东西。这是主要的 post 我一直在看 Passing an Object from an Activity to a Fragment

更新:我收到错误消息

1900-1900/com.chris.cv10aajproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.chris.cv10aajproject, PID: 1900
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chris.cv10aajproject/com.chris.cv10aajproject.editProperty}: android.view.InflateException: Binary XML file line #121: Error inflating class fragment

更新 2

编码为 activity Axml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.chris.cv10aajproject.editProperty">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="address"
    android:id="@+id/tvAddress"
    android:ems="10"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TOWN"
    android:id="@+id/tvTown"
    android:ems="10"
    android:layout_below="@+id/tvAddress"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="County"
    android:ems="10"
    android:id="@+id/tvCounty"
    android:layout_below="@+id/tvTown"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Post code"
    android:id="@+id/tvPostCode"
    android:ems="10"
    android:layout_below="@+id/tvCounty"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Asking Price"
    android:id="@+id/tvAskingPrice"
    android:layout_below="@+id/tvPostCode"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Property Summary"
    android:textStyle="bold"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/tvAddress"
    android:layout_alignEnd="@+id/tvAddress" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Current Offer"
    android:id="@+id/tvCurrentOffer"
    android:layout_below="@+id/tvAskingPrice"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="DoneUpValue"
    android:id="@+id/textView3"
    android:layout_below="@+id/tvCurrentOffer"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Notes"
    android:id="@+id/tvNotes"
    android:minHeight="200px"
    android:background="#276bffd0"
    android:scrollbars = "vertical"
    android:layout_below="@+id/textView3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/tvNotes"
    android:layout_alignEnd="@+id/tvNotes"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/fragBTL"
    class="com.chris.cv10aajproject.BtlFragment"
    android:layout_below="@+id/tvNotes"
    android:layout_alignBottom="@+id/fragFlip" />
<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvNotes"
    android:layout_alignRight="@+id/tvNotes"
    android:layout_alignEnd="@+id/tvNotes"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/fragFlip"
    class="com.chris.cv10aajproject.FlipFragment"
    android:layout_above="@+id/toggleFlipBtl" />

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="Change to Flip"
    android:textOn="Change to BTL"
    android:id="@+id/toggleFlipBtl"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:checked="false"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/tvAskingPrice"
    android:layout_toEndOf="@+id/tvAskingPrice" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"

    android:id="@+id/firstLine"

    android:layout_below="@+id/textView"
    android:layout_toRightOf="@+id/textView3"
    android:layout_toEndOf="@+id/textView3"
    android:layout_marginLeft="40dp"
    android:layout_marginStart="40dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Schedule"
    android:id="@+id/btnSchedule"
    android:onClick="GoToSchedule"
    android:layout_below="@+id/fragBTL"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calc Refurb"
    android:id="@+id/btnCalcRefurb"
    android:onClick="GoToRefurb"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"

    android:id="@+id/town23"
    android:layout_below="@+id/firstLine"
    android:layout_alignLeft="@+id/firstLine"
    android:layout_alignStart="@+id/firstLine"
 />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"

    android:id="@+id/county"
    android:layout_below="@+id/town23"
    android:layout_alignLeft="@+id/town23"
    android:layout_alignStart="@+id/town23" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"

    android:id="@+id/postCode123"
    android:layout_below="@+id/county"
    android:layout_alignLeft="@+id/county"
    android:layout_alignStart="@+id/county" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"

    android:id="@+id/currentOffer123"
    android:layout_below="@+id/askingPrice123"
    android:layout_alignLeft="@+id/askingPrice123"
    android:layout_alignStart="@+id/askingPrice123" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"

    android:id="@+id/askingPrice123"
    android:layout_below="@+id/postCode123"
    android:layout_alignLeft="@+id/postCode123"
    android:layout_alignStart="@+id/postCode123" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"

    android:id="@+id/doneUpValue123"
    android:layout_below="@+id/currentOffer123"
    android:layout_alignLeft="@+id/currentOffer123"
    android:layout_alignStart="@+id/currentOffer123" />

fragmentxml 代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout 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"

>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Flip Strategy"
    android:id="@+id/textView21"
    android:textStyle="bold"
    android:textSize="20sp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Maxium Offer"
    android:id="@+id/textView22"
    android:textSize="15sp"
    android:layout_below="@+id/textView21"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint=" Max Offer "
    android:textSize="15sp"
    android:id="@+id/MaxOfferFlip"
    android:layout_below="@+id/textView21"
    android:layout_centerHorizontal="true"
    android:background="#47ff4620" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Buyer Incentive"
    android:id="@+id/textView23"
    android:textSize="15sp"
    android:layout_below="@+id/textView22"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="4"
    android:id="@+id/IncentivePer"
    android:hint="Percent"
    android:textSize="15sp"
    android:layout_below="@+id/MaxOfferFlip"
    android:layout_alignLeft="@+id/MaxOfferFlip"
    android:layout_alignStart="@+id/MaxOfferFlip"
    android:background="#3409f6ff" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint=" Incentive "
    android:textSize="15sp"
    android:id="@+id/textIncentive"
    android:background="#47ff4620"
    android:layout_below="@+id/MaxOfferFlip"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="23dp"
    android:layout_marginEnd="23dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Refurb Cost"
    android:textSize="15sp"
    android:id="@+id/textView24"
    android:layout_below="@+id/textView23"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint="    Refurb    "
    android:textSize="15sp"
    android:id="@+id/refurb"
    android:background="#47ff4620"
    android:layout_below="@+id/IncentivePer"
    android:layout_alignRight="@+id/MaxOfferFlip"
    android:layout_alignEnd="@+id/MaxOfferFlip" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Holding Time cost"
    android:textSize="15sp"
    android:id="@+id/textView25"
    android:layout_below="@+id/textView24"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint="    Time       "
    android:textSize="15sp"
    android:id="@+id/holdingTime"
    android:background="#47ff4620"
    android:layout_below="@+id/refurb"
    android:layout_alignRight="@+id/refurb"
    android:layout_alignEnd="@+id/refurb" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Profit required"
    android:textSize="15sp"
    android:id="@+id/textView26"
    android:layout_below="@+id/textView25"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="4"
    android:id="@+id/profit"
    android:hint="Profit"
    android:textSize="15sp"
    android:background="#3409f6ff"
    android:layout_below="@+id/holdingTime"
    android:layout_alignLeft="@+id/IncentivePer"
    android:layout_alignStart="@+id/IncentivePer" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Buyiing Selling Fee"
    android:id="@+id/textView27"
    android:textSize="15sp"
    android:layout_below="@+id/textView26"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="4"
    android:id="@+id/buySellFee"
    android:hint="Fee"
    android:textSize="15sp"
    android:background="#3409f6ff"
    android:layout_below="@+id/profit"
    android:layout_alignLeft="@+id/profit"
    android:layout_alignStart="@+id/profit" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Monthly Mortage Cost"
    android:id="@+id/textView7"
    android:textSize="15sp"
    android:layout_below="@+id/textView27"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint=" Mortage Cost "
    android:textSize="15sp"
    android:id="@+id/MonthMortFlip"
    android:background="#47ff4620"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/holdingTime"
    android:layout_alignStart="@+id/holdingTime" />

片段javaClass public static FlipFragment newInstance(属性 属性 ){ FlipFragment 片段 = new FlipFragment(); Bundle bundle = new Bundle();

    bundle.putParcelable(PROPERTY_KEY,property);
    fragment.setArguments(bundle);
    return fragment ;
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_flip,container,false);
    mProperty =(Property) getArguments().getParcelable(PROPERTY_KEY);

       refurbCost = (TextView) getView().findViewById(R.id.refurb);

          String refurbCost2 =" " + mProperty.getRefurbCost();
        refurbCost.setText(refurbCost2);
    // return view;


    // mProperty = editActivity.getProperty();
    //if (getActivity().getIntent().getExtras() != null) {
    //  mProperty = getActivity().getIntent().getParcelableExtra(editProperty.PAR_KEY3);
    //}

    return view;

}
  @Override
public void onAttach(Activity myActivity) {
    super.onAttach(myActivity);
    this.editActivity = (editProperty) myActivity;
}

}

我有一个 activity A,它有两个片段,一个是隐藏的,一个是根据切换按钮显示的。

我有一个自定义对象被传递到我的 activity A 使用 parcelable。来自另一个 activity B. 我想将这个对象传递给来自 activity A 的两个片段用户将在片段上输入一些数据,这些数据将调用对象 class 中的方法,然后一旦这个完成我想把对象传回去。

我的应用程序在我尝试打开时一直崩溃 activity A(这只是在我尝试将对象传递给片段之后才开始发生,如果我将其注释掉它可以正常打开)

这是我在 activity A

中添加到我的 setupView 方法中的代码
 android.support.v4.app.FragmentTransaction ft =   
getSupportFragmentManager().beginTransaction();
    Fragment fragment = FlipFragment.newInstance(mProperty);
    ft.replace(R.id.flip_fragment,fragment);
    ft.commit();

这是我片段中的代码 java class

public static FlipFragment newInstance(Property property ){
    FlipFragment fragment = new FlipFragment();
    Bundle bundle = new Bundle();

    bundle.putParcelable(PROPERTY_KEY,property);
    fragment.setArguments(bundle);
    return fragment ;
}

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup     container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_flip,container,false);
    mProperty =(Property) getArguments().getParcelable(PROPERTY_KEY);

        refurbCost = (TextView) getView().findViewById(R.id.refurb);

            String refurbCost2 =" " + mProperty.getRefurbCost();
        refurbCost.setText(refurbCost2);

您不能在 onCreateView 方法中使用 getView(),它将 return 为 null。您在 onCreateView() 结束时 return 的视图将成为将由 getView() return 编辑的视图。相反,应该使用您膨胀的视图。

View view = inflater.inflate(R.layout.fragment_flip,container,false);
refurbCost = (TextView) view.findViewById(R.id.refurb);