Cannot acces ActivityCompatApi23 when trying to use FragmentActivity

Viewed 6502

I'm trying to use a ViewPager on my smartwatch, but I keep getting an error when trying to rebuild/run/debug my application. I'm using a FragmentActivity, which is where the error occurs. I searched stackoverflow and tutorial websites to see what could be the problem, and a lot of results are related to the build.gradle files. However I tried pretty much everything I stumbled upon, but the error didn't change once.

The error:

Error: Cannot acces ActivityCompatApi23

The error happens on this line, coming from the piece of code below.

public class WearMainActivity extends FragmentActivity {

WearMainActivity.java

package be.ehb.dt.finalwork_lievenluyckx_v001;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.List;
import java.util.Vector;

/**
 * Created by Lieven on 14/08/17.
 */

public class WearMainActivity extends FragmentActivity {


    private PagerAdapter pagerAdapter;

    /* (non-Javadoc)
      * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
      */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.viewpager);
        //initialsie the pager
        initialisePaging();
    }

    /**
     * Initialise the fragments to be paged
     */
    private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, Monitor.class.getName()));
        fragments.add(Fragment.instantiate(this, CurrentSongOverviewWear.class.getName()));
        this.pagerAdapter  = new MyPagerAdapter(getSupportFragmentManager(), fragments);

        ViewPager pager = (ViewPager) findViewById(R.id.viewpager_container);
        pager.setAdapter(this.pagerAdapter);
    }
}

build.gradle (WEAR)

apply plugin: 'com.android.application'

android {
    allprojects {
        repositories {
            jcenter()
            maven {
                url "https://maven.google.com"
            }
        }
    }

    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "be.ehb.dt.finalwork_lievenluyckx_v001"
        minSdkVersion 25
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile 'com.android.support:wear:26.0.0'
    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.google.android.support:wearable:2.0.3'
    compile 'com.google.android.gms:play-services:11.0.4'
    provided 'com.google.android.wearable:wearable:2.0.3'
    compile 'com.android.support:support-v4:26.0.0-alpha1'
}
4 Answers

Following Eugen Pechanec's explanation (as a comment below the original post) fixed the problem for me:

All support libraries need to be same version. If your compile SDK is 25, it's 25.4.0. If your compile SDK is 26, it's 26.0.1. Don't mix them.

My issue was conflicting versions of the support library being imported. One of my submodule projects was referencing an older version of the support library. Updating them to match fixed the issue. You can check your dependencies using the gradle command:

gradlew -p [YOUR_MODULE] dependencies

Where [YOUR_MODULE] is the name of your main module

Please match your gradle version and gradle build version and set the config like this:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

classpath 'com.android.tools.build:gradle:3.0.1'

to make it work.

Related