newInstance(); recieves null in the fragment

3926 views android
5

Good Day.

I have a problem when it comes to sending variable data from Activity to Fragment.

Here is my code in

Activity.java

    if (savedInstanceState == null) {
        Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
        getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                new Fragment_Voter_Home()).commit();
        navigationView.setCheckedItem(R.id.voter_home);
    }

then in

Fragment.java

public class Fragment_Voter_Home extends Fragment {

private static final String ARG_STUDNO = "stud_no";

private String student_no;

public static Fragment_Voter_Home newInstance(String student_no) {

    Fragment_Voter_Home fragment = new Fragment_Voter_Home();
    Bundle args = new Bundle();
    args.putString(ARG_STUDNO, student_no);
    fragment.setArguments(args);
    return fragment;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    View v = inflater.inflate(R.layout.fragment_voter_home, container, false);
    TextView textView = v.findViewById(R.id.voter_fragment_textview);

    if (getArguments() != null) {
        student_no = getArguments().getString(ARG_STUDNO);
    }

    textView.setText("Welcome, "+student_no);

    return v;

}
}

This is my Fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">

<TextView
    android:id="@+id/voter_fragment_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center_vertical|center_horizontal|center"
    android:text="Home"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    </FrameLayout>

Then after running this is what I get,

"Welcome, null"

I already tried the solutions in this thread Send a string value from Activity to Fragment in Android but none of them works.

I am new to android development. Thank you.

answered question

2 Answers

13

Because your code is not correct, change your code to

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                fragment).commit();

instead of

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                new Fragment_Voter_Home()).commit();

posted this
10

What's wrong is you instantiated the fragment like this:

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);

Then you didn't pass it into the FragmentTransaction, instead you passed a new Fragment_Voter_Home().

This makes your instantiated fragment which contain the passed argument unused then what you passed into the fragment stack contains uninitialized arguments which resulted in null.

posted this

Have an answer?

JD

Please login first before posting an answer.