Skip to content Skip to sidebar Skip to footer

Android: Bundle Nullpointerexception

I'm working on an app I need to send some data from ListFragment to ItemFragment. I use bundle but I get nullpointerexception. I should mention that i'm a newbie and this is my fir

Solution 1:

To put bundle in Fragment you should use a static funtion newInstance(Bundle bundle) in your ItemFragment:

publicstatic ItemFragment newInstance(Bundle bundle) {
    ItemFragmentmyFragment=newItemFragment();

    myFragment.setArguments(bundle);

    return myFragment;
}


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

    Bundlebundle= getArguments();
    // DO SOMETHINGreturnsuper.onCreateView(inflater, container, savedInstanceState);
}

And use it you call:

Bundlebundle=newBundle();
//put data in bundleItemFragmentitemfrag= ItemFragment.newInstance(bundle);

Solution 2:

In ItemFragment, you should write in this way

BundlebundledData=this.getArguments();
        if (getArguments() != null) {
          selectedId = bundledData.getLong("selectedId");
        }

And you may add Toast inside the if block to check whether the selectedId get displayed or not.

Edited

ItemFragment.java

privatelong selId;


     @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            Viewedit_details= inflater.inflate(R.layout.edit_staff_list, container, false);
            Bundlebundle=this.getArguments();
            if (getArguments() != null) {
               selId = bundle.getLong("selectedId");
                Toast.makeText(getActivity(), selId, Toast.LENGTH_LONG).show();

            }

Solution 3:

Try edit your code in listFragment to this.

if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
    int position = recyclerView.getChildLayoutPosition(child);    
    selectedItem = mItems.get(position);        
    itemId = mItems.get(position).getId();

    ItemFragment itemFragment = new ItemFragment();
    Bundle bundle = new Bundle();
    bundle.putLong("selectedId", itemId);
    itemFragment.setArguments(bundle);

    openFragment(itemFragment, "MyApp");
}

returnfalse;

The problem with your code is you passed in a new instance of ItemFragment to openFragment() function instead of the one you created and added with a bundle.

Solution 4:

You are creating two instances of the same fragment, setting the arguments on the second one, and showing the first one which doesn't have any arguments. Hence, the NullPointerException when you try to extract argument in onCreateView.

...
openFragment(newItemFragment(), "MyApp"); // Show first fragmentItemFragmentif=newItemFragment(); // Instantiate second fragmentBundlebundle=newBundle();
bundle.putLong("selectedId", itemId);
if.setArguments(bundle); // arguments go to the second one | The one which is never even displayed to the user.
...

Solution 5:

After hours messing around with the code, trial and error, tried different codes, nguyen's code and the code here https://stackoverflow.com/a/16036693/5563156 gives me the idea. these codes seems to get the job done

ListFragment.java

mRecyclerView.addOnItemTouchListener(newRecyclerView.OnItemTouchListener() {
        @OverridepublicbooleanonInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
            Viewchild= recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

            if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
                intposition= recyclerView.getChildLayoutPosition(child);
                selectedItem = mItems.get(position);
                itemId = mItems.get(position).getId();
                //pass selected item's ID to Main ActivityIntenti=newIntent(getActivity(), MainActivity.class);
                i.putExtra("selectedId", itemId);
                startActivity(i);
                Toast.makeText(getActivity(), "id = " + itemId, Toast.LENGTH_SHORT).show();

            }
            returnfalse;
        }

In MainActivity's OnCreate

if(savedInstanceState == null){
        //receives selected item's ID passed from ListFragmentIntentintent= getIntent();
        longselId= intent.getLongExtra("selectedId", 0);

         if (selId > 0) {
             //send the selected item ID to ItemFragmentBundlebundle=newBundle();
             bundle.putLong("selectedId", 0);
             ItemFragmentfrag=newItemFragment();
         openFragment(ItemFragment.newInstance(selId), "Update Item");
         frag.setArguments(bundle);
         }}

ItemFragment.java

publicstatic ItemFragment newInstance(long selId) {
    ItemFragmentfragment=newItemFragment();

    if (selId > 0) {
        Bundlebundle=newBundle();
        bundle.putLong("selectedId", selId);
        fragment.setArguments(bundle);
    }
    return fragment;
}

In ItemFragment's OnCreateView

Bundleargs= getArguments();
    if (args != null && args.containsKey("selectedId")) {
        selId = args.getLong("selectedId");
    }

Thanks to everyone who posted their codes! Appreciate the help.

Post a Comment for "Android: Bundle Nullpointerexception"