0

I was trying to create base activity using this answer with Hilt. But this throws an error.

error: [Hilt]
public abstract class ActivityBase<V extends androidx.lifecycle.ViewModel, B extends androidx.viewbinding.ViewBinding> extends androidx.appcompat.app.AppCompatActivity {
                ^
  Found unimplemented abstract methods, [getViewBinding()], in an abstract module, com.baseproject.base.ActivityBase. Did you forget to add a Dagger binding annotation (e.g. @Binds)?

Base Activity:

@InstallIn(SingletonComponent::class)
@Module
abstract class ActivityBase<V : ViewModel, B: ViewBinding> : AppCompatActivity() {

    var mViewModel: V? = null
    protected var mBinding: B? = null


    private fun getViewModelClass(): Class<V> {
        val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0]
        return type as Class<V>
    }

 
    abstract fun getViewBinding(): B

    lateinit var navHostFragment: NavHostFragment
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init()
        setContentView(mBinding?.root)
        setUpHideKeyBoard()
    }

    protected open fun init() {
        mBinding = getViewBinding()
        mViewModel = ViewModelProvider(this).get(getViewModelClass())
    }
}

Main Activity:

@AndroidEntryPoint
class MainActivity : ActivityBase<ViewModelBase, ActivityMainBinding>() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
     override fun getViewBinding() = ActivityMainBinding.inflate(layoutInflater)
    }

if I remove both @Module and @InstallIn(SingletonComponent::class) annotations then it throws below error.

error: method getActivityFactory in class DefaultViewModelFactories cannot be applied to given types;
    return DefaultViewModelFactories.getActivityFactory(this);
                                    ^
  required: ComponentActivity,Factory
  found: Hilt_MainActivity<viewBinding>
  reason: actual and formal argument lists differ in length
  where B is a type-variable:
    B extends ViewBinding declared in class Hilt_MainActivity
Mehta
  • 1,228
  • 1
  • 9
  • 27
  • 1
    Why is `ActivityBase` annotated with `@Module` and Why is it part of `SingletonComponent` . What you trying to do? This seems messed up . – ADM May 14 '21 at 06:51
  • @ADM, I want to create my MainActivity with entry point and it I remove both annotation from ActivityBase then it throws error. – Mehta May 14 '21 at 07:00
  • @Mehta As @ADM mentioned, those `@Module` and `@InstallIn` annotations on your Activity make no sense. – skywall May 15 '21 at 09:18
  • @skywall, if I remove both annotations from my abstract activity then it throws another error. Refer my updated question. – Mehta May 17 '21 at 05:25
  • How did you fixed this issue? I am also facing similar problem – Madhu Jun 16 '22 at 14:14

0 Answers0