【发布时间】:2020-07-11 12:39:33
【问题描述】:
我在 Android 中有一个包含框架布局的活动。其中一个框架布局是用片段膨胀的。
在片段的onResume() 中,调用了一个在Activity 中实现的监听器。
侦听器然后调用片段的方法。此时,片段的引用发生了 NPE。
这种情况很少发生,但至少被复制了 2 次。
我怀疑问题与活动和片段的生命周期有关。
当 Activity 仍处于生命周期的 onCreate() 步骤中时,就会对片段进行引用,这可能在片段初始化之前。
我的分析正确吗?如何预防 NPE?
这是代码(请记住,我已经重命名了很多代码并删除了看起来不相关的部分):
活动:
FoodsActivity extends AppCompatActivity implements FruitStateFragment.OnAppleSelectedListener {
private Context mContext;
private FruitsManager mFruitsManagr;
private FruitStateFragment mFruitStateFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foods_activity);
if (savedInstanceState != null) {
return;
}
if (mContext == null) {
mContext = getApplicationContext();
}
mFruitsManagr = FruitsManager.get(mContext);
if (findViewById(R.id.fl_fruits_status) != null) {
mFruitStateFragment = new FruitStateFragment();
mFruitStateFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fl_fruits_status, mFruitStateFragment, "FruitStateFragment").commit();
}
mFruitsManagr.setApple(0);
}
@Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof FruitStateFragment) {
FruitStateFragment fruitStateFragment = (FruitStateFragment) fragment;
fruitStateFragment.setOnAppleSelectedListener(this);
}
}
public void onAppleSelected(Integer appleNum) {
FruitsManager fManager = FruitsManager.get(mContext);
fManager.setApple(appleNum);
// NPE on mFruitStateFragment
mFruitStateFragment.updateBasketUi(fManager.getActiveBasketName());
}
}
片段:
FruitStateFragment extends Fragment {
private Context mContext;
FruitsManager mFruitsManagr;
OnAppleSelectedListener mAppleCallback;
public void setOnAppleSelectedListener(OnAppleSelectedListener callback) {
this.mAppleCallback = callback;
}
public interface OnAppleSelectedListener {
void onAppleSelected(Integer appleNum);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mContext == null) {
mContext = getActivity().getApplicationContext();
}
mFruitsManagr = FruitsManager.get(mContext);
}
@Override
public void onResume() {
super.onResume();
mAppleCallback.onAppleSelected(mFruitsManagr.getActiveApple());
}
void updateBasketUi(String basketName) {
}
}
经理:
public class FruitsManager {
private static FruitsManager sMe;
private Context mContext;
private static int mActiveApple = 0;
private static String mActiveBasketName = "";
private FruitsManager(Context context) {
mContext = context;
initInterfaces();
}
public int getActiveApple() {
return mActiveApple;
}
public int getActiveBasketName() {
return mActiveBasketName;
}
}
日志:
D FRUITS: 0001 onCreate (FoodsActivity%onCreate:)
D FRUITS: 0002 onCreate (FruitStateFragment%onCreate:)
D FRUITS: 0003 onCreateView (FruitStateFragment%onCreateView:)
D FRUITS: 0004 onActivityCreated (FruitStateFragment%onActivityCreated:)
D FRUITS: 0005 initViews (FruitStateFragment%initViews:)
D FRUITS: 0006 onResume (FruitStateFragment%onResume:)
E AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {com.hi/FoodsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.hi.FruitStateFragment.updateBasketUi(java.lang.String)' on a null object reference
【问题讨论】:
标签: android android-fragments android-activity listener activity-lifecycle