Reputation: 2315
I am trying to pass data between fragments on call back. In my first fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = ...
categoryList = (ArrayList<Category>) getArguments().getSerializable("categorylist");
return v;
}
@Override
public void callbackCall(ArrayList<Category> list)
{
Toast.makeText(getActivity(), "call back",
Toast.LENGTH_SHORT).show();
}
My interface:
public interface CategoryListCallBack {
void callbackCall(ArrayList<Category> list);
}
In my second fragment when I successfully updated the database, I am pulling the data again to be pass back to the first fragment:
private CategoryListCallBack mcallback;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = ...
mcallback = (CategoryListCallBack) getFragmentManager().findFragmentByTag("CategoryListActivity");
return v;
}
final CategoryListActivity categoryFragment = new CategoryListActivity();
new GetAllCategoriesAsyncTask(
new GetAllCategoriesAsyncTask.OnRoutineFinished() {
public void onFinish() {
getFragmentManager().popBackStack();
/*Bundle bundle = new Bundle();
FragmentTransaction it = getFragmentManager().beginTransaction();
bundle.putSerializable("categorylist", GetAllCategoriesAsyncTask.categoryList);
categoryFragment.setArguments(bundle);
it.replace(R.id.frame,categoryFragment);
it.addToBackStack("tag");
it.commit();*/
mcallback.callbackCall(GetAllCategoriesAsyncTask.categoryList);
}
}).execute();
However, I am getting this error message:
java.lang.NullPointerException: Attempt to invoke interface method 'void Controller.CategoryListCallBack.callbackCall(java.util.ArrayList)' on a null object reference
Any ideas? Thanks in advance.
Edit
When my list view item onclick, I am calling the second fragment:
Bundle bundle = new Bundle();
EditCategoryActivity editFragment = new EditCategoryActivity();
FragmentTransaction et = getFragmentManager().beginTransaction();
bundle.putSerializable("category", model);
editFragment.setArguments(bundle);
et.replace(R.id.frame,editFragment);
et.addToBackStack("tag");
et.commit();
In my second fragment onCreate():
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_edit_category, container, false);
mcallback = (CategoryListCallBack) getFragmentManager().findFragmentByTag("CategoryListActivity");
//v.setCategoryListCallBack(mcallback);
// get category details pass from previous intent
category = (Category) getArguments().getSerializable("category");
// button onclick insert data to database, then call the async task and call back to first fragment
return v;
}
Upvotes: 0
Views: 67
Reputation: 257
In your second fragment
mcallback = (CategoryListCallBack) getFragmentManager().findFragmentByTag("CategoryListActivity");
this line must be returning null ,try this code
public void setCategoryListCallBack(CategoryListCallBack categoryListCallBack){
this.mcallback = categoryListCallBack;
}
set interface reference from this method ,and always do null check on interface reference
final CategoryListActivity categoryFragment = new CategoryListActivity();
new GetAllCategoriesAsyncTask(
new GetAllCategoriesAsyncTask.OnRoutineFinished() {
public void onFinish() {
if(mcallback != null){
mcallback.callbackCall(getContext());
}
getFragmentManager().popBackStack();
/*Bundle bundle = new Bundle();
FragmentTransaction it = getFragmentManager().beginTransaction();
bundle.putSerializable("categorylist", GetAllCategoriesAsyncTask.categoryList);
categoryFragment.setArguments(bundle);
it.replace(R.id.frame,categoryFragment);
it.addToBackStack("tag");
it.commit();*/
}
}).execute();
Add listener for second fragment like:
Bundle bundle = new Bundle();
EditCategoryActivity editFragment = new EditCategoryActivity();
FragmentTransaction et = getFragmentManager().beginTransaction();
editFragment.setCategoryListCallBack(this);
bundle.putSerializable("category", model);
editFragment.setArguments(bundle);
et.replace(R.id.frame,editFragment);
et.addToBackStack("tag");
et.commit();
and remove this line from onCreatView()
mcallback = (CategoryListCallBack) getFragmentManager().findFragmentByTag("CategoryListActivity");
Upvotes: 1