Cannot Initialize ViewModel inside Fragment - android

Hi im using view model class to pass some data to my fragment, I have done the same using activity without having any issue, but using fragment I'm unable to initialize view model class.
mDriversWallet = new ViewModelProvider(requireActivity()).get(DriversWalletViewModel.class);
And it keeps giving me error,
error: no suitable constructor found for ViewModelProvider(FragmentActivity)mDriversWallet = new ViewModelProvider(requireActivity()).get(DriversWalletViewModel.class);
It would be very helpful if some can correct me,

Hi i just found the solution, just to let know others don't get me as a complete idiot :D.This project i have been worked with android studio 3.2 and compileSDKversion 25.So recently i have updated android studio to 3.6 which is latest and compileSDKversion to 28, this viewModel problem occur besause i forget to migrate my code to androidX .After successfully migrate code to androidX problem solved, Thanks and sorry for such a dump question...This answer will help some other who dump like me :D

Related

Android studio kotlin, cannot directly select ID without findViewById()

I'm new to android studio and I'm leaning it from this video
At 50:45 he says that we can directly use the id without findViewById (), for him it shows just import that Id, but when I tried the same I'm not getting what he is getting. Can anyone tell me what's wrong, is that feature removed in the latest update??
"kotlinx.android.synthetic is no longer a recommended practice. Removing in favour of explicit findViewById"
So, this feature`s been deprecated almost a year ago.
The new recommended way of working with view tree is View Binding.
Or you could write some lazy extension function that uses findViewById under the hood.
You can use the Kotlin Android Extensions check here or with the help of data binding or view binding you can just use the id directly in to your java activity class
The way you're talking about is Kotline Extensions for view, for which you apply it in the build.gradle file using apply plugin: 'kotlin-android-extensions', sync the project as prompted by the Android studio and it starts working.
But, Kotlin extensions has also been deprecated for view binding which is null-safe, also easy to implement and better.
To read about view binding, read the Android documentation on ViewBinding here and about migrating here. You can also go with this article to help with setting up Android view binding.

How can i solve this )

Please Help. Im following tutorial on youtube (channel: coding in flow). Its about making a todo app in kotlin using android studio, and i got stuck on this:
taskDao.update(task.copy(completed = isChecked))
Error: Unresolved reference copy.
Tryed a lot of things but didnt help.
task is most likely an instance of a data class which has its own copy() method. If you have access to this class, make sure it is a data class and not a class.

ViewModelProviders.of(getActivity()) "Cannot resolve method of(android.app.Activity)"

I'm currently trying to share data between my two fragments with the help of a view model Android ViewModels.
In the this article they use "ViewModelProviders.of(getActivity()).get(SharedViewModel.class);" in the Fragment to get the model.
My Problem is, that when I write the exact same line into my fragment, I get a "Cannot resolve method of(android.app.Activity)" error
model = ViewModelProviders.of(getActivity()).get(UserAccountViewModel.class);
Can some one help me with this ?
ViewModelProviders requires that you use FragmentActivity (or something derived from it), not Activity, as the base class of your activities.
You might also get a similiar error like Cannot resolve method 'of(android.support.v4.app.FragmentActivity)' from ViewModelProviders.of(getActivity()) if you were trying to import androidx in your gradle file when your app is still using android.support.v4
This is different to the question being asked but is in the same ballpark and ranks first in Google when I had the problem, so I'm posting it!
Just cast getActivity() to specific activity. It works for me. For example:
model = ViewModelProviders.of((MainActivity)getActivity()).get(UserAccountViewModel.class);

Has MvxCachingFragmentCompatActivity disappeared?

I'm trying to upgrade to MvvmCross 5.2 and get a syntax error on the MvxCachingFragmentCompatActivity. Has this class disappeared?
As explained in the blog for 5.2: https://www.mvvmcross.com/mvvmcross-52-release/
We've refactored the Android presenter and there is no need anymore for a special Activity like MvxCachingFragmentCompatActivity. The adviced Activity to use now is the MvxAppCompatActivity.

AndroidAnnotations cannot find symbol class

I'm using AndroidAnnotations in an Android Studio gradle project. I currently get error output from AA during compilation that says:
cannot find symbol class MyActivity_
The error output does not prevent building the application - its not really a compile error because the class is there, it seems that it is just an unfortunate timing issue with the compilation process.
Is there anything I can do to avoid these false-positive errors from AA? When there are "fake" errors shown every time I compile, its very easy to miss the real errors.
I had same error. To solve it I have revert my last changes and it has worked again.
I think it was either wrong optimized import(you must import generated classes eg. xxx_) or I injected layout by id what was not existed in the layout xml
Update
I figured it. My problem was what I had use private mofidier instead of proteced in
#ViewById(R.id.list)
private ListView list;
Try to see if you missed to fix some errors in the class MainActivity or in someone of his Bean member that you have annoted.
The problem doesn't have to be in MainActivty, but it is probably because of a private modifier which is used with Android Anotations (in injection, method declaration etc) somewhere in your code

Categories

Resources