gpt4 book ai didi

android - 不能从静态上下文中引用非静态方法 getChildFragmentManager()

转载 作者:搜寻专家 更新时间:2023-11-01 09:46:31 24 4
gpt4 key购买 nike

在 fragment 中,我尝试在静态方法中使用 getChildFragmentManager()。但它显示错误并建议我使方法不是静态的。

Non static method getChildFragmentManager() cannot be referenced from a static context

fragment :

public class C_Fragment extends android.support.v4.app.Fragment

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



return inflater.inflate(R.layout.fragment_a, container, false);
}

static void updateProductList(){
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (getChildFragmentManager().findFragmentByTag("kz.dreamwings.unimax.fragments.C_Category_A_prudacts")!=null) {
// C_Category_A_prudacts.adapter.notifyDataSetChanged();
Fragment frg = null;
frg = getChildFragmentManager().findFragmentByTag("kz.dreamwings.unimax.fragments.C_Category_A_prudacts");
final FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
}
}, 300);
}

最佳答案

是的,您不能从静态上下文访问非静态上下文。您正在尝试在静态方法中获取 fragment 实例,您应该在静态方法之外执行此操作,以便您可以访问 getChildFragmentManager()

private static Fragment fragment;

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

fragment = getChildFragmentManager().findFragmentByTag("kz.dreamwings.unimax.fragments.C_Category_A_prudacts");

//.... other code
}

static void updateProductList(){
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (fragment!=null) {
//.... other code
}
}
}, 300);
}

关于android - 不能从静态上下文中引用非静态方法 getChildFragmentManager(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37873948/

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