BeginnerJavaDev
BeginnerJavaDev

Reputation: 57

Passing a object from my activity to my fragment

I have been trying to resolve this issue for a while now have tried to follow a few examples on here but i seem to be missing something. this is the main post i've been looking at Passing an Object from an Activity to a Fragment

UPDATE: error message i get

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

UPDATE 2

code to 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" />

Code for 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" />

Fragment 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;
}

}

i have a activity A that has two fragments one is hidden and one is displayed depending on a toggle button.

i have a custom object that is passed to my activity A using parcelable. from another activity B. i want to pass this object to both fragments from activity A the user will input some data on the fragments which will call methods in the object class, then once this is done i want to pass the object back.

my application keeps crashing when i try to open activity A ( this has only started to happen after i've tried to pass the object to the fragments if i comment it out it opens fine)

This is the code i added to my setupView method in activity A

 android.support.v4.app.FragmentTransaction ft =   
getSupportFragmentManager().beginTransaction();
    Fragment fragment = FlipFragment.newInstance(mProperty);
    ft.replace(R.id.flip_fragment,fragment);
    ft.commit();

this is the code i have in my fragment 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);

Upvotes: 2

Views: 291

Answers (1)

Joey Harwood
Joey Harwood

Reputation: 981

You cannot use getView() within the onCreateView method, it will return null. The view that you return at the end of onCreateView() becomes the view that will be returned by getView(). Instead, the view you inflated should be used.

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

Upvotes: 1

Related Questions