i get many crash reports from the Play Store from devices using Android 7.0 and 7.1 (no other versions) which I can't reproduce on any of my 7.x devices (and emulators).
Crash:
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1473)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6123)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)
Caused by: java.lang.NullPointerException:
at com.package.de.ui.base.BaseActivity.onCreate (BaseActivity.java:53)
at com.package.de.ui.startup.StartupActivity.onCreate (StartupActivity.java:26)
at android.app.Activity.performCreate (Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2612)
So far so good - a simple NullPointer Exception, but unfortunately its not that simple. (At least for me)
My StartUpActivity
which inherits the BaseActivity
looks like this:
public class StartupActivity extends BaseActivity implements StartupView {
private StartupComponent startupComponent;
@Inject
StartupPresenter startupPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupComponent(MyApplication.get().getAppComponent()); <- Crash
setContentView(R.layout.activity_startup);
startupPresenter.bindView(this);
}
public void setupComponent(AppComponent appComponent) {
startupComponent = DaggerStartupComponent.builder()
.appComponent(appComponent)
.startupModule(new StartupModule())
.build();
startupComponent.inject(this);
}
Also nothing too fancy here.
BaseActivity:
public abstract class BaseActivity extends AppCompatActivity {
@Inject
protected UserSessionManager userSessionManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.get().getAppComponent().inject(this); <- Crash
}
So I have a BaseActivity
which will be injected into the AppComponent
.
Finally the Application
class:
public class MyApplication extends MultiDexApplication {
private AppComponent appComponent;
private static MyApplication appInstance;
public static MyApplication get() {
return appInstance;
}
@Override
public void onCreate() {
super.onCreate();
appInstance = this;
setupAppComponent();
}
private void setupAppComponent() {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.userApiModule(new UserApiModule())
.build();
appComponent.inject(this);
}
public synchronized AppComponent getAppComponent() {
if (appComponent == null)
setupAppComponent();
return appComponent;
}
}
I know this is a lot of code but I want to make sure you have all the information you need. I would be more than happy for any suggestions regarding my Dagger2 Setup. There has to be some issue but I can't figure it out.
edit: If anyone runs into this problem. Take a look here: RuntimeException with Dagger 2 on Android 7.0 and Samsung devices This might be your solution.