我的活动启动模型:singleTop
在onCreate中:
mTabIcon = findViewById(R.id.m_tab_icon);
在onNewIntent中:
mTabIcon.setSelected(true);
但发生 NullPointerException :
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setSelected(boolean)' on a null object reference
com.myActivity.onNewIntent
android.app.Instrumentation.callActivityOnNewIntent (Instrumentation.java:1229)
android.app.Instrumentation.callActivityOnNewIntent (Instrumentation.java:1241)
android.app.ActivityThread.deliverNewIntents (ActivityThread.java:2648)
android.app.ActivityThread.performResumeActivity (ActivityThread.java:3189)
android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3242)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2584)
android.app.ActivityThread.access$800 (ActivityThread.java:176)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1452)
android.os.Handler.dispatchMessage (Handler.java:111)
android.os.Looper.loop (Looper.java:192)
android.app.ActivityThread.main (ActivityThread.java:5619)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:976)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:771)
有人可以给我一些解决办法吗?代码类似:
private ImageView mTabIcon;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabIcon = findViewById(R.id.m_tab_icon);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
mTabIcon.setSelected(true);
}