Java ANDROID:无法解析方法 getSupportActionBar()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25100179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:37:50  来源:igfitidea点击:

ANDROID: Cannot resolve method getSupportActionBar()

javaandroid

提问by Thomas Riddler

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.

这件事让我发疯。我似乎无法让我的应用程序正确编译。我通过复制 jar 文件,将它们放入 libs 文件夹,右键单击并将它们添加为库,在我的应用程序中添加了支持库。

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

我已经尝试扩展 ActionBaractivity 并且仍然没有得到该方法的认可。

I am using Android studio version 0.8.2.

我正在使用 Android Studio 版本 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

构建.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.+'
    }

采纳答案by Giancarlo

you need to change Activity to ActionBarActivity

您需要将 Activity 更改为 ActionBarActivity

public class mainActivity extends ActionBarActivity

public class mainActivity extends ActionBarActivity

ActionBarActivity has been deprecated so please use the following

ActionBarActivity 已被弃用,因此请使用以下内容

public class mainActivity extends AppCompatActivity

public class mainActivity extends AppCompatActivity

回答by C James

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

您的活动需要扩展 ActionBarActivity(提供 getSupportActionBar)。

回答by michaelcarrano

  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.
  1. 从 libs 目录中删除支持库 jar,因为您已经在 gradle 文件中定义了支持库。

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

    支持库已更新,因此我会将您的 gradle 依赖项更新为:compile 'com.android.support:support-v4:19.1.+'

  2. Android Studio 已经会通知您,但请确保您与 gradle 同步。

  3. 不是从 Activity 扩展,而是从 ActionBarActivity 扩展。

回答by ehab mousa

This will solve the problem:

这将解决问题:

import android.support.v7.widget.Toolbar;

回答by Nilesh Panchal

It is simple, just extend AppCompatActivity instead of Activity.

很简单,只是扩展 AppCompatActivity 而不是 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

例外:您需要在此活动中使用 Theme.AppCompat 主题(或后代)

while using AppCompatActivity

使用 AppCompatActivity 时