0

I am trying to inject an AlertDialog to MainActivity like

class MainActivity : BaseActivity() {

    @Inject
    val alertStore:AlertStore;  //propery must be initialized or abstract Error

    private lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Where as AppComponent.kt

@Component(
    modules = [
        AndroidInjectionModule::class,
        AppModule::class
    ]
)
@Singleton
interface AppComponent : AndroidInjector<App> {

    @Component.Builder
    interface Builder {

        fun addContext(@BindsInstance context: Context): Builder

        fun build(): AppComponent
    }
}

AppModule.kt

@Module
class AppModule {
    @Singleton
    @Provides
    fun provideAlertStore(context: Context) : AlertStore = AlertStore(context)
}

App.kt

class App : DaggerApplication() {

    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        return DaggerAppComponent.builder().addContext(this).build()
    }
}

I am new to dagger. So bear with me. I Think I am missing context . Suggest me and please explain a little bit So I understand this concept as well

Do I have to inject MainActivity as well to get Alert or my context is missing.

File Under build folder Generated

@kotlin.Metadata(mv = {1, 1, 15}, bv = {1, 0, 3}, k = 1, d1 = {"\u0000&\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\u0018\u00002\u00020\u0001B\u0005\u00a2\u0006\u0002\u0010\u0002J\u0012\u0010\t\u001a\u00020\n2\b\u0010\u000b\u001a\u0004\u0018\u00010\fH\u0014R\u0016\u0010\u0003\u001a\u00020\u00048\u0006X\u0087\u0004\u00a2\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006R\u000e\u0010\u0007\u001a\u00020\bX\u0082.\u00a2\u0006\u0002\n\u0000\u00a8\u0006\r"}, d2 = {"Lcom/example/myapplication/MainActivity;", "Lcom/example/myapplication/common/BaseActivity;", "()V", "alertStore", "Lcom/example/myapplication/common/AlertStore;", "getAlertStore", "()Lcom/example/myapplication/common/AlertStore;", "viewModel", "Lcom/example/myapplication/home/MainViewModel;", "onCreate", "", "savedInstanceState", "Landroid/os/Bundle;", "app_debug"})
public final class MainActivity extends com.example.myapplication.common.BaseActivity {
    @org.jetbrains.annotations.NotNull()
    @javax.inject.Inject()
    private final com.example.myapplication.common.AlertStore alertStore = null;
    private com.example.myapplication.home.MainViewModel viewModel;
    private java.util.HashMap _$_findViewCache;

    @org.jetbrains.annotations.NotNull()
    public final com.example.myapplication.common.AlertStore getAlertStore() {
        return null;
    }

    @java.lang.Override()
    protected void onCreate(@org.jetbrains.annotations.Nullable()
    android.os.Bundle savedInstanceState) {
    }

    public MainActivity() {
        super();
    }
}

EDIT after Alexey Suggestion

After updating my variable like

@Inject lateinit var alertStore: AlertStore

The error gone but on running app below error occur

Method threw 'kotlin.UninitializedPropertyAccessException' exception.

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300

1 Answers1

0

Your issue is not directly related to Dagger, but rather to Kotlin.

Sinc you use a val property, you need to initialize it with a value. Otherwise it would be uninitialized while the contract states that it's non-null. An abstract property obviously wouldn't work, so you might need to declare you property differently and inject with a setter.

@set:Inject internal lateinit var alertStore: AlertStore
tynn
  • 38,113
  • 8
  • 108
  • 143
  • tynn I have tried your solution along with @AlexeyRomanov but now UnintializedPropertyAccessException occur. Plz tell me how to pass context to AlertStore in AppModule.kt – Zar E Ahmer Nov 21 '19 at 09:20
  • Have you created an `ActivityModule`? – tynn Nov 21 '19 at 09:24
  • No , Do I have to inject Activity as well ? Why? And from where AlertStore get Context to be initialized?? – Zar E Ahmer Nov 21 '19 at 09:24
  • 1
    This exceeds the scope of this question. Try to google some tutorials. This article seems like a good start https://medium.com/@iammert/new-android-injector-with-dagger-2-part-1-8baa60152abe – tynn Nov 21 '19 at 09:26