Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot acces ActivityCompatApi23 when trying to use FragmentActivity

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'
}
like image 497
Lieven Luyckx Avatar asked Aug 14 '17 15:08

Lieven Luyckx


People also ask

Should I use activity or AppCompatActivity?

If you want the backported Material Design look, use AppCompatActivity. If not, but you want nested fragments, use FragmentActivity. If not, use Activity.

What is a FragmentActivity?

A FragmentActivity is a subclass of Activity that was built for the Android Support Package. The FragmentActivity class adds a couple new methods to ensure compatibility with older versions of Android, but other than that, there really isn't much of a difference between the two.

What is difference between activity and AppCompatActivity in Android?

Activity is the basic one. Based on Activity , FragmentActivity provides the ability to use Fragment . Based on FragmentActivity , AppCompatActivity provides features to ActionBar.


2 Answers

I just experienced a similar issue when using AppCompatActivity:

error: cannot access ActivityCompatApi23

I fixed by setting a specific support library version:

compile 'com.android.support:appcompat-v7:26.+' <-- Old
compile 'com.android.support:appcompat-v7:26.0.2' <-- New
like image 197
Tanner Perrien Avatar answered Sep 19 '22 19:09

Tanner Perrien


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.

like image 36
Lieven Luyckx Avatar answered Sep 20 '22 19:09

Lieven Luyckx