0
此代碼在某些設備上崩潰,但在其他設備上正常工作。片段中的OnPrepareOptionsMenu在設備上具有不同的行爲
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
boolean isSelectModeActive = mRecyclerAdapter.getSelectedItemCount() > 0;
mCallback.onChangeMenuInSelectionMode(menu, isSelectModeActive);
}
附上回調onAttach方法:
@Override
public void onAttach(Context context) {
super.onAttach(context);
Activity activity = (Activity) context;
try {
mCallback = (iSongsListFragmentEventListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement iSongsListFragmentEventListener");
}
}
堆棧跟蹤說mCallback是空的onPrepareOptions方法,但是爲什麼呢? 我在我的nexus 4和所有模擬器上測試了這段代碼 - 沒有崩潰,但有些用戶崩潰了。
Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'void chuprin.serg.com.autotagger.fragments.SongsListFragment$iSongsListFragmentEventListener.onChangeMenuInSelectionMode(android.view.Menu, boolean)' on a null object reference
at chuprin.serg.com.autotagger.fragments.SongsListFragment.onPrepareOptionsMenu(SongsListFragment.java:292)
對您的對象'mCallback'進行空檢查。 – Exception