当我恢复到片段时,会发生此异常。这是堆栈跟踪
Non-fatal Exception: java.lang.RuntimeException: Unable to resume activity {com.beco.ibeco/com.beco.ibeco.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3212)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3243)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1465)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
at com.beco.ibeco.app.store.StoreListFragment.onResume(StoreListFragment.java:141)
at android.support.v4.app.Fragment.performResume(Fragment.java:2235)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1346)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2898)
at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:223)
at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:509)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:498)
at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:172)
at android.app.Activity.performResume(Activity.java:6389)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3197)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3243)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1465)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是代码
@Override
public void onResume() {
super.onResume();
Sample.getApp().setMallId(null);
Sample.getApp().setStoreId(null);
if(mMenu != null && mMenu.hasVisibleItems() && mMenu.getItem(0) != null)
mMenu.getItem(0).getIcon().setColorFilter(getResources().getColor(R.color.sample_black), PorterDuff.Mode.SRC_ATOP);
}
我无法找到这个NPE发生的地方。任何人请帮助我。提前谢谢!
答案 0 :(得分:0)
修改强>
要解决此问题,我还必须检查mMenu.getItem(0).getIcon() != null
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if(mMenu != null && mMenu.hasVisibleItems() && mMenu.getItem(0) != null && mMenu.getItem(0).getIcon() != null)
mMenu.getItem(0).getIcon().setColorFilter(ContextCompat.getColor(getContext(),R.color.beco_black), PorterDuff.Mode.SRC_ATOP);
}