将对象从我的活动传递到我的片段

时间:2015-04-21 15:46:54

标签: android android-fragments android-activity parcelable

我一直试图解决这个问题一段时间,现在试着在这里举几个例子,但我似乎错过了一些东西。这是我一直关注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

活动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(Property property){         FlipFragment fragment = 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;
}

}

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

我有一个使用parcelable传递给我的活动A的自定义对象。来自另一个活动B.我希望将此对象传递给活动A中的两个片段,用户将在片段上输入一些数据,这些片段将调用对象类中的方法,然后一旦完成,我想将对象传回。

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

这是我在活动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类

中的代码
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);

1 个答案:

答案 0 :(得分:1)

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

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