13

This thing is driving me nuts. I can't seem to get my app to compile correctly. I have added the support libraries in my app by copying the jar files, dropping them into libs folder, right click and adding them as library.

I have tried extending ActionBaractivity and nothing still not getting that method recognized.

I am using Android studio version 0.8.2.

Class

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class mainActivity extends Activity implements AdapterView.OnItemClickListener {

    private DrawerLayout drawerLayout;
    private ListView listView;
    private String[] navMenuArray;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navMenuArray = getResources().getStringArray(R.array.navmenu);
        listView =(ListView) findViewById(R.id.drawerList);
        listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, navMenuArray));
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //handles what happens when an item in the menu is clicked
        Toast.makeText(this,navMenuArray[position]+" was selected", Toast.LENGTH_SHORT).show();
        selectItem(position);
    }
    public void selectItem(int position){
        listView.setItemChecked(position, true);
        setTitle(navMenuArray[position]);
    }
    public void setTitle(String title){
        getSupportActionbar().setTitle(title);
    }
}

MANIFEST

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name=".mainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'

    defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 14
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    compileOptions{
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

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

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:support-v4:18.0.+'
        compile 'com.android.support:appcompat-v7:18.0.+'
    }
Thomas Riddler
  • 393
  • 3
  • 7
  • 18

6 Answers6

38

you need to change Activity to ActionBarActivity

public class mainActivity extends ActionBarActivity

ActionBarActivity has been deprecated so please use the following

public class mainActivity extends AppCompatActivity

Nirmal Raj
  • 729
  • 6
  • 18
Giancarlo
  • 745
  • 5
  • 13
1

Your activity needs to extend ActionBarActivity (which provides getSupportActionBar).

C B J
  • 1,838
  • 16
  • 22
0
  1. Remove the support library jars from the libs directory as you already defined the support library in your gradle file.

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

    The support library has been updated so I would update your gradle dependencies to: compile 'com.android.support:support-v4:19.1.+'

  2. Android Studio will already notify you but make sure you sync with gradle.

  3. Rather than extending from Activity, extend from ActionBarActivity.
michaelcarrano
  • 1,316
  • 2
  • 12
  • 19
0

It is simple, just extend AppCompatActivity instead of Activity.

Please Note that you need to change theme to avoid

Exception : You need to use a Theme.AppCompat theme (or descendant) with this activity

while using AppCompatActivity

Nilesh
  • 1,013
  • 14
  • 21
0

extends Activity, if you are using the appcompat it needs to be extends AppCompatActivity

Robert Greene
  • 151
  • 1
  • 5
-1

This will solve the problem:

import android.support.v7.widget.Toolbar;
Mike Laren
  • 8,028
  • 17
  • 51
  • 70