How to get the getArguments data from a fragment?

opu picture opu · Apr 24, 2016 · Viewed 7.8k times · Source

I am using a fragment for my application.and I also add a bundle into my fragments but when I want to get the added String from the bundle the compiler shows me a null pointer error like as follows

Process: com.softmaker.gazi_opu.personalhealthcare, PID: 3253
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
            at com.softmaker.gazi_opu.personalhealthcare.fragment.GeneralInformationFragment.setValue(GeneralInformationFragment.java:67)
            at com.softmaker.gazi_opu.personalhealthcare.fragment.GeneralInformationFragment.onCreateView(GeneralInformationFragment.java:59)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

And my added code and getting the added data code are given bellow

public static GeneralInformationFragment getInstance(String profileID){
        Bundle bundle = new Bundle();
        bundle.putString("profileId",profileID);
        GeneralInformationFragment generalFragment = new GeneralInformationFragment();
        generalFragment.setArguments(bundle);
        return generalFragment;
    }

Now for getting data from the this fragment within onCreateMethod

String id = getArguments().getString("profileId");
        Log.e("ID :", "" + id.toString());

Can anyone suggest me. where am I making mistake ?

Answer

Amir picture Amir · Apr 24, 2016

I always pass data like this and work perfectly :

public class CategoryResultFragment extends BaseListFragment {

    private static final String CATEGORY_ID = "EXTRA_CATEGORY_ID";
    private static final String CATEGORY_SLUG = "EXTRA_CATEGORY_SLUG";

    private String mCategoryId;
    private String mCategorySlug;

    public static CategoryResultFragment newInstance(String categoryId, String categorySlug) {
        Bundle bundle = new Bundle();
        bundle.putString(CATEGORY_ID, categoryId);
        bundle.putString(CATEGORY_SLUG, categorySlug);
        CategoryResultFragment categoryResultFragment = new CategoryResultFragment();
        categoryResultFragment.setArguments(bundle);
        return categoryResultFragment;
    }


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

        if (getArguments() != null) {
            mCategoryId = getArguments().getString(CATEGORY_ID);
            mCategorySlug = getArguments().getString(CATEGORY_SLUG);
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}