我的应用程序根据支持Android SDK的设备使用Intent Service和Job Intent Service组件。在服务组件内部,我试图访问应用程序扩展类内的一些公共定义的函数。在尝试访问此功能时,有时会引发NullPointer异常。
MyApplication.class
public class MyApplication extends MultiDexApplication {
private static final String TAG = MyApplication.class.getName();
@Inject
SharedPreferences mSharedPreferences;
private BaseApplicationComponent applicationComponent;
private static MyApplication mInstance;
{
mInstance = this;
}
@Override
public void onCreate() {
super.onCreate();
applicationComponent = DaggerBaseApplicationComponent.builder()
.baseAndroidModule(new BaseAndroidModule(this))
.remoteDataModule(new RemoteDataModule())
.localDataModule(new LocalDataModule())
.build();
applicationComponent.inject(this);
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
public BaseApplicationComponent getComponent(){return
applicationComponent;
}
}
服务类OnCreate()
@Override
public void onCreate() {
super.onCreate();
MyApplication.getInstance().getComponent().plus(new NotificationServiceModule(this))
.inject(this);
}
崩溃日志:
java.lang.RuntimeException:
at android.app.ActivityThread.handleCreateService
(ActivityThread.java:3554)
at android.app.ActivityThread.-wrap4 (Unknown Source)
at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1786)
at android.os.Handler.dispatchMessage (Handler.java:105)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6942)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run
(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException:
at com.xyz.app.MyApplicarion.getComponent (MyApplication.java:93)
at com.xyz.app.pushnotification.NotificationJobService.onCreate
(NotificationJobService.java:83)
at android.app.ActivityThread.handleCreateService
(ActivityThread.java:3544)
at android.app.ActivityThread.-wrap4 (Unknown Source)
at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1786)
at android.os.Handler.dispatchMessage (Handler.java:105)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6942)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run
(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
我的应用程序或访问代码中是否存在任何问题,或者提供任何解决此问题的建议。