Bobby
Bobby

Reputation: 659

AppCompatActivity Error

I am new to Android studio from Eclipse and trying to tweek setup. When I add and Activity (blank activity) it generates the java file but it comes up with extends AppCompatActivity which gives errors.Most of my project use ActionBarActivity or Activity. Is there a setting that I need to address to gernerate these files and eliminate errors ?

Here is the file generated by adding a activity

package com.example.jerry.els2015;

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

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Here is the gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "com.example.jerry.els2015"
    minSdkVersion 19
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}

Upvotes: 1

Views: 6638

Answers (4)

tachyonflux
tachyonflux

Reputation: 20211

Based on whether or not you have a dependency on the support library in your build.gradle, Android Studio will either generate your activity with extends AppCompatActivity or extends Activity.

Upvotes: 2

N.T.
N.T.

Reputation: 2611

As you might already know, AppCompatActivity is part of the support library. I wonder if you have the library downloaded. You could open the SDK manager and scroll all the way to the end to make sure you have it:

enter image description here After that you need to include it as a dependency to the gradle build file for the module (not project). In your build.gradle file you need to add: compile 'com.android.support:appcompat-v7:22.2.1'

Upvotes: 1

kandroidj
kandroidj

Reputation: 13932

if your using Gradle

dependencies{
     compile 'com.android.support:appcompat-v7:21.0.0'
}

Upvotes: 0

hasan
hasan

Reputation: 24205

At the left projects pane. you will see a project with the name "support-v7-appcompat" on it. double click that to open it.

Upvotes: 0

Related Questions