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;

privatevoidselectItem(int position) {

    FragmentManagerfragmentManager= getSupportFragmentManager();
    FragmentTransactionfragmentTransaction= fragmentManager.beginTransaction();
    StringfragmentTag= String.valueOf(position);

    FragmentBasefragment= (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) {
    FragmentBasefragment=null;
    if (position == 0) {
        fragment = newQuickNoteFragment();
        Bundleargs=newBundle();
        args.putInt(QuickNoteFragment.ARG_nOTERACTIVITY_NUMBER, position);
        fragment.setArguments(args);

    } elseif (position == 1) {
        fragment = newPendViewPager();
        Bundleargs=newBundle();
        fragment.setArguments(args);
    }
    return fragment;
}

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

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

Delete the code below from FragmentBase:

privateFragmentActivity mActivity; // I changed it to FragmentActivity// because Activity was not working, and// my `NavDrawer` is a FragmentActivity.publicvoidsetContext(FragmentActivity activity) {
    mActivity = mActivity;
}

publicFragmentActivitygetContext() {
    return mActivity;
}

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

classFragmentBaseextendsFragment {

 }

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:

@OverridepublicvoidonDetach() {
    super.onDetach();
    DataModel.getInstance().setOnItemAddedHandler(null);
} 

update1

@OverridepublicvoidonItemAdded(Object data) {
    // to add itemStringstring = String.valueOf(data);
    planetsList.add(createPlanet("planet", string));
    simpleAdpt.notifyDataSetChanged();
}

Post a Comment for "Nullpointerexception When Trying To Update A Listview From Another Fragment"