error: no suitable method found for method ViewModelProviders.of(Fragment,Factory)












0















Trying to provde ViewModel via ViewModelProviers.of() to my activity.



My activity extends AppCompatActivity but I still see this error:




error: no suitable method found for
of(StartActivity,StartViewModelFactory) method
ViewModelProviders.of(Fragment,Factory) is not applicable (argument
mismatch; StartActivity cannot be converted to Fragment) method
ViewModelProviders.of(FragmentActivity,Factory) is not applicable
(argument mismatch; StartActivity cannot be converted to
FragmentActivity)




Dependency Injection is provided with Dagger 2. Below is my Activity class StartActivity:



public class StartActivity extends AppCompatActivity {

@Inject
StartViewModelFactory startViewModelFactory;
StartModelView startModelView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
startModelView = ViewModelProviders.of(this, startViewModelFactory).
get(StartViewModel.class);

}


If I understand correctly you should be able to send in an activity in ViewModelProviders.of() method if they extend AppCompatActivity or Fragment. But I get this error. Any help would be appreciated.










share|improve this question





























    0















    Trying to provde ViewModel via ViewModelProviers.of() to my activity.



    My activity extends AppCompatActivity but I still see this error:




    error: no suitable method found for
    of(StartActivity,StartViewModelFactory) method
    ViewModelProviders.of(Fragment,Factory) is not applicable (argument
    mismatch; StartActivity cannot be converted to Fragment) method
    ViewModelProviders.of(FragmentActivity,Factory) is not applicable
    (argument mismatch; StartActivity cannot be converted to
    FragmentActivity)




    Dependency Injection is provided with Dagger 2. Below is my Activity class StartActivity:



    public class StartActivity extends AppCompatActivity {

    @Inject
    StartViewModelFactory startViewModelFactory;
    StartModelView startModelView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    AndroidInjection.inject(this);
    super.onCreate(savedInstanceState);
    startModelView = ViewModelProviders.of(this, startViewModelFactory).
    get(StartViewModel.class);

    }


    If I understand correctly you should be able to send in an activity in ViewModelProviders.of() method if they extend AppCompatActivity or Fragment. But I get this error. Any help would be appreciated.










    share|improve this question



























      0












      0








      0








      Trying to provde ViewModel via ViewModelProviers.of() to my activity.



      My activity extends AppCompatActivity but I still see this error:




      error: no suitable method found for
      of(StartActivity,StartViewModelFactory) method
      ViewModelProviders.of(Fragment,Factory) is not applicable (argument
      mismatch; StartActivity cannot be converted to Fragment) method
      ViewModelProviders.of(FragmentActivity,Factory) is not applicable
      (argument mismatch; StartActivity cannot be converted to
      FragmentActivity)




      Dependency Injection is provided with Dagger 2. Below is my Activity class StartActivity:



      public class StartActivity extends AppCompatActivity {

      @Inject
      StartViewModelFactory startViewModelFactory;
      StartModelView startModelView;

      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
      AndroidInjection.inject(this);
      super.onCreate(savedInstanceState);
      startModelView = ViewModelProviders.of(this, startViewModelFactory).
      get(StartViewModel.class);

      }


      If I understand correctly you should be able to send in an activity in ViewModelProviders.of() method if they extend AppCompatActivity or Fragment. But I get this error. Any help would be appreciated.










      share|improve this question
















      Trying to provde ViewModel via ViewModelProviers.of() to my activity.



      My activity extends AppCompatActivity but I still see this error:




      error: no suitable method found for
      of(StartActivity,StartViewModelFactory) method
      ViewModelProviders.of(Fragment,Factory) is not applicable (argument
      mismatch; StartActivity cannot be converted to Fragment) method
      ViewModelProviders.of(FragmentActivity,Factory) is not applicable
      (argument mismatch; StartActivity cannot be converted to
      FragmentActivity)




      Dependency Injection is provided with Dagger 2. Below is my Activity class StartActivity:



      public class StartActivity extends AppCompatActivity {

      @Inject
      StartViewModelFactory startViewModelFactory;
      StartModelView startModelView;

      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
      AndroidInjection.inject(this);
      super.onCreate(savedInstanceState);
      startModelView = ViewModelProviders.of(this, startViewModelFactory).
      get(StartViewModel.class);

      }


      If I understand correctly you should be able to send in an activity in ViewModelProviders.of() method if they extend AppCompatActivity or Fragment. But I get this error. Any help would be appreciated.







      android mvvm viewmodel dagger-2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 14:55







      Carlton

















      asked Nov 21 '18 at 14:37









      CarltonCarlton

      93421436




      93421436
























          1 Answer
          1






          active

          oldest

          votes


















          1














          you should be using



             .get(StartModelView.class);


          and not



           .get(StartViewModelFactory.class);


          the factory provided to of() is used by android to generate an instance of your ViewModel subclass






          share|improve this answer
























          • Thanks, That is true. I added it in my code & this question. But the problem is still there. What could it persists? Could it have something to do with dependency injection?

            – Carlton
            Nov 21 '18 at 14:56













          • It looks like you were correct. There was also another underlying problem with my modelviewfactory. I get a "uses uncecked or unsafe operations" from this line @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { return (T) new StartModelView(getAllWalletsInteractor); } DO you know why?

            – Carlton
            Nov 21 '18 at 15:19













          • that's normal, it is the way generics work on java.

            – Blackbelt
            Nov 21 '18 at 15:24













          • Should I add @SuppressWarning annotation to it?

            – Carlton
            Nov 21 '18 at 15:26











          • as you wish. It is a warning not an error

            – Blackbelt
            Nov 21 '18 at 15:45













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414435%2ferror-no-suitable-method-found-for-method-viewmodelproviders-offragment-factor%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          you should be using



             .get(StartModelView.class);


          and not



           .get(StartViewModelFactory.class);


          the factory provided to of() is used by android to generate an instance of your ViewModel subclass






          share|improve this answer
























          • Thanks, That is true. I added it in my code & this question. But the problem is still there. What could it persists? Could it have something to do with dependency injection?

            – Carlton
            Nov 21 '18 at 14:56













          • It looks like you were correct. There was also another underlying problem with my modelviewfactory. I get a "uses uncecked or unsafe operations" from this line @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { return (T) new StartModelView(getAllWalletsInteractor); } DO you know why?

            – Carlton
            Nov 21 '18 at 15:19













          • that's normal, it is the way generics work on java.

            – Blackbelt
            Nov 21 '18 at 15:24













          • Should I add @SuppressWarning annotation to it?

            – Carlton
            Nov 21 '18 at 15:26











          • as you wish. It is a warning not an error

            – Blackbelt
            Nov 21 '18 at 15:45


















          1














          you should be using



             .get(StartModelView.class);


          and not



           .get(StartViewModelFactory.class);


          the factory provided to of() is used by android to generate an instance of your ViewModel subclass






          share|improve this answer
























          • Thanks, That is true. I added it in my code & this question. But the problem is still there. What could it persists? Could it have something to do with dependency injection?

            – Carlton
            Nov 21 '18 at 14:56













          • It looks like you were correct. There was also another underlying problem with my modelviewfactory. I get a "uses uncecked or unsafe operations" from this line @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { return (T) new StartModelView(getAllWalletsInteractor); } DO you know why?

            – Carlton
            Nov 21 '18 at 15:19













          • that's normal, it is the way generics work on java.

            – Blackbelt
            Nov 21 '18 at 15:24













          • Should I add @SuppressWarning annotation to it?

            – Carlton
            Nov 21 '18 at 15:26











          • as you wish. It is a warning not an error

            – Blackbelt
            Nov 21 '18 at 15:45
















          1












          1








          1







          you should be using



             .get(StartModelView.class);


          and not



           .get(StartViewModelFactory.class);


          the factory provided to of() is used by android to generate an instance of your ViewModel subclass






          share|improve this answer













          you should be using



             .get(StartModelView.class);


          and not



           .get(StartViewModelFactory.class);


          the factory provided to of() is used by android to generate an instance of your ViewModel subclass







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:43









          BlackbeltBlackbelt

          129k23221246




          129k23221246













          • Thanks, That is true. I added it in my code & this question. But the problem is still there. What could it persists? Could it have something to do with dependency injection?

            – Carlton
            Nov 21 '18 at 14:56













          • It looks like you were correct. There was also another underlying problem with my modelviewfactory. I get a "uses uncecked or unsafe operations" from this line @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { return (T) new StartModelView(getAllWalletsInteractor); } DO you know why?

            – Carlton
            Nov 21 '18 at 15:19













          • that's normal, it is the way generics work on java.

            – Blackbelt
            Nov 21 '18 at 15:24













          • Should I add @SuppressWarning annotation to it?

            – Carlton
            Nov 21 '18 at 15:26











          • as you wish. It is a warning not an error

            – Blackbelt
            Nov 21 '18 at 15:45





















          • Thanks, That is true. I added it in my code & this question. But the problem is still there. What could it persists? Could it have something to do with dependency injection?

            – Carlton
            Nov 21 '18 at 14:56













          • It looks like you were correct. There was also another underlying problem with my modelviewfactory. I get a "uses uncecked or unsafe operations" from this line @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { return (T) new StartModelView(getAllWalletsInteractor); } DO you know why?

            – Carlton
            Nov 21 '18 at 15:19













          • that's normal, it is the way generics work on java.

            – Blackbelt
            Nov 21 '18 at 15:24













          • Should I add @SuppressWarning annotation to it?

            – Carlton
            Nov 21 '18 at 15:26











          • as you wish. It is a warning not an error

            – Blackbelt
            Nov 21 '18 at 15:45



















          Thanks, That is true. I added it in my code & this question. But the problem is still there. What could it persists? Could it have something to do with dependency injection?

          – Carlton
          Nov 21 '18 at 14:56







          Thanks, That is true. I added it in my code & this question. But the problem is still there. What could it persists? Could it have something to do with dependency injection?

          – Carlton
          Nov 21 '18 at 14:56















          It looks like you were correct. There was also another underlying problem with my modelviewfactory. I get a "uses uncecked or unsafe operations" from this line @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { return (T) new StartModelView(getAllWalletsInteractor); } DO you know why?

          – Carlton
          Nov 21 '18 at 15:19







          It looks like you were correct. There was also another underlying problem with my modelviewfactory. I get a "uses uncecked or unsafe operations" from this line @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { return (T) new StartModelView(getAllWalletsInteractor); } DO you know why?

          – Carlton
          Nov 21 '18 at 15:19















          that's normal, it is the way generics work on java.

          – Blackbelt
          Nov 21 '18 at 15:24







          that's normal, it is the way generics work on java.

          – Blackbelt
          Nov 21 '18 at 15:24















          Should I add @SuppressWarning annotation to it?

          – Carlton
          Nov 21 '18 at 15:26





          Should I add @SuppressWarning annotation to it?

          – Carlton
          Nov 21 '18 at 15:26













          as you wish. It is a warning not an error

          – Blackbelt
          Nov 21 '18 at 15:45







          as you wish. It is a warning not an error

          – Blackbelt
          Nov 21 '18 at 15:45






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414435%2ferror-no-suitable-method-found-for-method-viewmodelproviders-offragment-factor%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules