gpt4 book ai didi

java - 当 Android fragment onCallBack 带有数据时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 13:09:12 24 4
gpt4 key购买 nike

我正在尝试在回调时在 fragment 之间传递数据。在我的第一个 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();
}

我的界面:

public interface CategoryListCallBack {
void callbackCall(ArrayList<Category> list);
}

在我成功更新数据库后的第二个 fragment 中,我再次拉取数据以传递回第一个 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();

但是,我收到此错误消息:

java.lang.NullPointerException: Attempt to invoke interface method 'void Controller.CategoryListCallBack.callbackCall(java.util.ArrayList)' on a null object reference

有什么想法吗?提前致谢。

编辑

当我的 ListView 项 onclick 时,我正在调用第二个 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();

在我的第二个 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;
}

最佳答案

在你的第二个 fragment 中

mcallback = (CategoryListCallBack) getFragmentManager().findFragmentByTag("CategoryListActivity");

此行必须返回 null ,尝试此代码

public void setCategoryListCallBack(CategoryListCallBack categoryListCallBack){
this.mcallback = categoryListCallBack;
}

从此方法设置接口(interface)引用,并始终对接口(interface)引用进行空检查

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();

为第二个 fragment 添加监听器,例如:

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();

并从onCreatView()

中删除这一行
mcallback = (CategoryListCallBack) getFragmentManager().findFragmentByTag("CategoryListActivity");

关于java - 当 Android fragment onCallBack 带有数据时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44016686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com