Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access Activity methods from AppCompatActivity

Tags:

java

android

When I extend AppCompatActivity I cannot use the methods from Activity such as finish() and setTheme() although the Android developer page says I should be able to use them.

Here is my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.test"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v13:23.1.0'

}

Here is my code that is giving me errors:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.DarkTheme);
        if (Sections.isLocked(this)) {
            Data.showSectionLocked(Data.last);
            finish();
        } else {
            Data.last = this;
        }
    }
}

Is there any way to fix this?

EDIT: It seems like this is an issue with android.support.v4.app.FragmentActivity.

Upon Ctrl+clicking on AppCompatActivity I noticed that it cannot extend Fragment Activity for some reason:

Image 1

Image 2

like image 319
CorruptComputer Avatar asked Dec 03 '25 15:12

CorruptComputer


2 Answers

I had same issue and fix for me it was changed buildToolsVersion to "23.0.3" in build.gradle file.

like image 156
Jozka Jozin Avatar answered Dec 06 '25 04:12

Jozka Jozin


AppCompatActivity inherit from FragmentActivity, FragmentActivity inherit from Activity. So you can use finish() without any issue.

Checkout you import correct AppCompatActivity and rebuild your project.

like image 25
Ming C Avatar answered Dec 06 '25 03:12

Ming C