Cannot acces ActivityCompatApi23 when trying to use FragmentActivity

廉价感情. 提交于 2019-12-10 12:54:12

问题


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'
}

回答1:


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



回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/45677543/cannot-acces-activitycompatapi23-when-trying-to-use-fragmentactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!