Skip to content Skip to sidebar Skip to footer

NullPointerException When Trying To Update A ListView From Another Fragment

I have two fragments, QuickNoteFragment and HistoryFragment, which are a part of a NavigationDrawer (this is the FragmentActivity they a bound to). In QuickNoteFragment I have a b

Solution 1:

Because you want HistoryFragment keep active after you went to QuickNoteFragment, so you should not replace the fragment, like the code below:

fragmentManager.beginTransaction().replace(R.id.content_frame, qnfragment).commit();

I have modified the Navigation_Drawer in order to make all the fragments keeping active.

private FragmentBase mCurrentFragment;

private void selectItem(int position) {

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    String fragmentTag = String.valueOf(position);

    FragmentBase fragment = (FragmentBase) fragmentManager.findFragmentByTag(fragmentTag);
    if (null == fragment) {
        fragment = createFragmentByPosition(position);
    }
    if (null == fragment)
        return;

    if (fragment.isAdded()) {
        fragmentTransaction.show(fragment);
    } else {
        fragmentTransaction.add(R.id.content_frame, fragment, fragmentTag);
    }

    if (mCurrentFragment != null) {
        fragmentTransaction.hide(mCurrentFragment);
    }
    mCurrentFragment = fragment;
    fragmentTransaction.commitAllowingStateLoss();

    mDrawerList.setItemChecked(position, true);
    setTitle(mNoterActivities[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

private FragmentBase createFragmentByPosition(int position) {
    FragmentBase fragment = null;
    if (position == 0) {
        fragment = new QuickNoteFragment();
        Bundle args = new Bundle();
        args.putInt(QuickNoteFragment.ARG_nOTERACTIVITY_NUMBER, position);
        fragment.setArguments(args);

    } else if (position == 1) {
        fragment = new PendViewPager();
        Bundle args = new Bundle();
        fragment.setArguments(args);
    }
    return fragment;
}

then, in onItemAdded in HistoryFragment, still use getActivity()

@Override
public void onItemAdded(Object data) {
    // to add item
    String string = String.valueOf(data);
    Toast.makeText(getActivity(), "Data: " + string, Toast.LENGTH_SHORT).show();
    planetsList.add(createPlanet("planet", string));
}

Delete the code below from FragmentBase:

private FragmentActivity mActivity; // I changed it to FragmentActivity
                                    // because Activity was not working, and
                                    // my `NavDrawer` is a FragmentActivity.

public void setContext(FragmentActivity activity) {
    mActivity = mActivity;
}

public FragmentActivity getContext() {
    return mActivity;
}

If you did not add other content into FragmentBase, it will be:

 class FragmentBase extends Fragment {

 }

Yes, it is empty, but I suggest to keep it.

At last, If HistoryFragment is at background, it may be detached by system when app go back to background. so unregister self from DataModel when detach:

@Override
public void onDetach() {
    super.onDetach();
    DataModel.getInstance().setOnItemAddedHandler(null);
} 

update1

@Override
public void onItemAdded(Object data) {
    // to add item
    String string = String.valueOf(data);
    planetsList.add(createPlanet("planet", string));
    simpleAdpt.notifyDataSetChanged();
}

Post a Comment for "NullPointerException When Trying To Update A ListView From Another Fragment"