相关疑难解决方法(0)

getActivity()在Fragment函数中返回null

我有一个像这样的公共方法的片段(F1)

public void asd() {
    if (getActivity() == null) {
        Log.d("yes","it is null");
    }
}
Run Code Online (Sandbox Code Playgroud)

是的,当我调用它(来自Activity)时,它是null ...

FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
F1 f1 = new F1();
transaction1.replace(R.id.upperPart, f1);
transaction1.commit();
f1.asd();
Run Code Online (Sandbox Code Playgroud)

它一定是我做错了,但我不知道那是什么

null android android-context android-fragments android-activity

182
推荐指数
7
解决办法
15万
查看次数

片段没有活动

我被问到一个面试问题:一个片段可以没有活动吗?我搜索了答案,但没有得到正确的答案和解释.有人可以帮忙吗?

android android-fragments

19
推荐指数
3
解决办法
7176
查看次数