10

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 Fragment
  • The method ViewModelProviders#of(Fragment) does exist and is accessible via the sdk.

Any idea on what I'm doing wrong ?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
merours
  • 4,076
  • 7
  • 37
  • 69

4 Answers4

17

Architecture Components uses the appcompat Fragment from the support library rather than the native one. Try changing your import for Fragment to

import android.support.v4.app.Fragment;

For historical reasons, there are two different Fragment classes. They have the same functionality but exist in two different packages. For details, see Why are there two Fragment classes in Android?

As of Feb 6, 2019:

Now there is a third Fragment class. If you are using the new AndroidX libraries, then do

import androidx.fragment.app.Fragment;

Be sure you use the correct Fragment class which is consistent with the rest of your dependencies.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • @fxm You will need to be careful with the rest of your app. My suggestion is to consistently use the support `Fragment` class across the board so that you don't run into similar issues elsewhere. – Code-Apprentice Jan 16 '18 at 22:58
  • I'll do so, but I admit I'm a bit confused by what looks like a duplicate to me. I'll have to read more I guess – merours Jan 16 '18 at 22:59
  • @fxm What duplicate do you mean? Are you referring to the two `Fragment` classes? – Code-Apprentice Jan 16 '18 at 23:00
  • Yes, components seems to be almost vanilla android when reading the doc and yet, there exist a somewhat different fragment class than in the original code – merours Jan 16 '18 at 23:04
  • 1
    @fxm Hopefully [this](https://stackoverflow.com/questions/48291270/why-are-there-two-fragment-classes-in-android/48291271#48291271) will clear up some of the confusion. The main reason for having two `Fragment` classes is historical. – Code-Apprentice Jan 16 '18 at 23:09
4

I have had the same problem, what the google documentation does not tell you is that you need to add the following dependencies in to your Build Gradle file and then "Sync"

implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
matshidis
  • 489
  • 5
  • 11
  • Links to the [documentation](https://developer.android.com/topic/libraries/architecture/adding-components) and [release notes](https://developer.android.com/jetpack/docs/release-notes) – jbuhacoff Jul 29 '18 at 09:02
1

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

Boken
  • 4,825
  • 10
  • 32
  • 42
akki
  • 405
  • 1
  • 6
  • 13
0

I have had the same problem, android studio automatically imported android.support.v4.app.Fragment; but I was using androidX. if your situation is same, you must use androidX imports everywhere

import androidx.fragment.app.Fragment;
a0x2
  • 1,995
  • 1
  • 18
  • 26