2

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);
Community
  • 1
  • 1
  • 1
    My guess is that you're not fully implementing Parcelable for your Property object. It's hard to tell without seeing your stack trace. – Joey Harwood Apr 21 '15 at 15:52
  • I've been successfully moving tobjects of the class throughout my application i don't get a stacktrace the application just closes – BeginnerJavaDev Apr 21 '15 at 15:53
  • Sorry im not very experienced – BeginnerJavaDev Apr 21 '15 at 15:53
  • Post your errors please as @MichaelJCaboose says. – Soham Apr 21 '15 at 15:53
  • Unable to start activity ComponentInfo{com.chris.cv10aajproject/com.chris.cv10aajproject.editProperty}: android.view.InflateException: Binary XML file line #121: Error inflating class fragment i think this is the error code i get in log cat :) – BeginnerJavaDev Apr 21 '15 at 16:20
  • 1846-1846/com.chris.cv10aajproject E/AndroidRuntime﹕ FATAL EXCEPTION: main – BeginnerJavaDev Apr 21 '15 at 16:23
  • 1
    Okay so it's having an issue inflating your fragment. The answer is definitely occurring either in your XML or in the onCreateView method. I'd recommend editing your post to include both the R.layout.fragment_flip XML, and the full onCreateView method. – Joey Harwood Apr 21 '15 at 16:27
  • :@MichaeljCaboose Ok will do 1 second, also thank you for your time i do appreciate all the advice – BeginnerJavaDev Apr 21 '15 at 16:30
  • 1
    No problem, I like helping out new developers learn. – Joey Harwood Apr 21 '15 at 16:31
  • possible duplicate of [Fragment-Activity communication, and inter-fragment communication](http://stackoverflow.com/questions/12262769/fragment-activity-communication-and-inter-fragment-communication) – Jared Burrows Apr 21 '15 at 16:37
  • @Jared Burrows i tried to follow that post editActivity = new editProperty() ; mProperty = new Property(); mProperty = editActivity.getProperty() ; which lets me open up the class with out an error but when i try to add something to one of the text views i get the crash again refurbCost = (TextView) getView().findViewById(R.id.refurb); refurbCost.setText(" "+5); error 3025-3025/com.chris.cv10aajproject E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.chris.cv10aajproject, PID: 3025 java.lang.RuntimeException: Unable to start activity – BeginnerJavaDev Apr 21 '15 at 17:09
  • 1
    I'm not sure if this will solve it, but I do see one problem. You use getView() within the method onCreateView(). The value for getView() is set at the end of onCreateView() when you return it, so I'd expect this to be null. I'm honestly not sure why you're not getting a NullPointerException there. Use the view that you inflated, instead of getView() to findViewById(). If that works I'll post it as an answer. – Joey Harwood Apr 21 '15 at 17:20
  • @MichaelJCaboose thank you this and a mixture of the link jaredBurrows posted has worked i still get error message but the page opens and i add some info into one of the textViews i'll try it with my old setup with out using getview() and let you know – BeginnerJavaDev Apr 21 '15 at 17:29
  • @MichaelJcab Thanks i believe this has worked. i've managed to open the fragment and add a value from my object to a textView ( haven't done full testng) how do i give your comment as the answer? – BeginnerJavaDev Apr 21 '15 at 17:36
  • I'll add it as an answer. – Joey Harwood Apr 21 '15 at 17:39

1 Answers1

1

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);
Joey Harwood
  • 961
  • 1
  • 17
  • 27