I'm trying to go through the google tutorial on building good ap architecture. I'm using java 8 and SDK 26. Here is the code I have so far :
package test.me;
import android.app.Fragment;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
public class ChronoFragment extends Fragment {
private ViewModel my_model;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
my_model = ViewModelProviders.of(this).get(ViewModel.class); // <-- error on this
// and so on
}
}
The trouble is that so far, the SDK returns the following error:
Cannot resolve the method 'of(test.me.ChronoFragment)'.
I don't understand this since
ChronoFragment
is of type FragmentAny idea on what I'm doing wrong ?
I have faced the same problem while using Android X libraries and used following to resolved.
Gradle dependencies for Android X:
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0-alpha04"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.1.0-alpha04"`
Import:
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModel;
Reference - https://developer.android.com/jetpack/androidx/migrate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With