Java Android Studio 1.0.2 新项目 - 无法解析符号“ActionBarActivity”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27766025/
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-11 04:58:43  来源:igfitidea点击:

Android Studio 1.0.2 new project - Cannot resolve symbol 'ActionBarActivity'

javaandroidandroid-studioandroid-gradle-plugin

提问by 3dDi92

I'm very confused. Today I just reinstalled Android Studio Version 1.0.2 (and the Android SDK) because I had some problems yesterday when I created a new project. I hoped that I could solve them by just reinstall everything, but actually the problem still exists and I don't know how to solve it.

我很困惑。今天我刚刚重新安装了 Android Studio Version 1.0.2(和 Android SDK),因为我昨天在创建一个新项目时遇到了一些问题。我希望我可以通过重新安装所有东西来解决它们,但实际上问题仍然存在,我不知道如何解决它。

After I created a new project in Android Studio and switch to my MainActivity (I didn't changed anything in one of the files), I always get the following errors displayed in the file.

在 Android Studio 中创建一个新项目并切换到我的 MainActivity(我没有更改其中一个文件中的任何内容)后,我总是在文件中显示以下错误。

MainActivity.class:

MainActivity.class:

package com.android.testapplication;

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


public class MainActivity extends ActionBarActivity {

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


    @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_main, 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);
    }
}

Errors:

错误:

  • Cannot resolve symbol 'ActionBarActivity'
  • Cannot resolve method 'onCreate(android.os.Bundle)'
  • Cannot resolve method 'setContentView(int)'
  • Cannot resolve method 'getMenuInflater()'
  • Cannot resolve method 'setContentView(int)'
  • Cannot resolve method 'onOptionsItemSelected(android.view.MenuItem)'
  • 无法解析符号“ActionBarActivity”
  • 无法解析方法“onCreate(android.os.Bundle)”
  • 无法解析方法“setContentView(int)”
  • 无法解析方法“getMenuInflater()”
  • 无法解析方法“setContentView(int)”
  • 无法解析方法“onOptionsItemSelected(android.view.MenuItem)”

Project: build.gradle

项目:build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

Module: build.gradle

模块:build.gradle

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.android.testapplication"
        minSdkVersion 14
        targetSdkVersion 21
        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:21.0.3'
}

I already rebuild and cleaned my project and 'Sync Project with Gradle Files'but none of them fixed my issues.

我已经重建并清理了我的项目,'Sync Project with Gradle Files'但他们都没有解决我的问题。

The funny thing is, that I can compile and run my project on my smartphone…

有趣的是,我可以在我的智能手机上编译和运行我的项目......

PS: I guess it seems to be a gradle issue, because whenever I add a new library such as 'com.google.android.gms:play-services:6.5.87'and then try to import it 'import com.google.android.gms.gcm.GoogleCloudMessaging;'. I also get the 'Cannot resolve symbol…'error displayed. I didn't complied it with the GCM Service…

PS:我想这似乎是一个 gradle 问题,因为每当我添加一个新库,例如'com.google.android.gms:play-services:6.5.87'然后尝试导入它'import com.google.android.gms.gcm.GoogleCloudMessaging;'。我也得到'Cannot resolve symbol…'显示的错误。我没有遵守 GCM 服务...

Thanks for your help in advance :)

提前感谢您的帮助:)

回答by alang6d2

For everyone who needs it, you can find it here:

对于需要它的每个人,您可以在此处找到它:

Android Studio says "cannot resolve symbol" but project compiles

Android Studio 说“无法解析符号”但项目编译

If the link goes down:

如果链接断开:

File> Invalidate Caches / Restart...and Invalidate and Restartto solve the issue.

File>Invalidate Caches / Restart...Invalidate and Restart解决问题。

回答by Whales

Ill suggest that you download a fresh version of Android SDK Tools. It is possible that one (or more) of your build tools, platform may be corrupt. And point your Android Studio to the new sdk tools.

我建议您下载新版本的 Android SDK 工具。您的一个(或多个)构建工具、平台可能已损坏。并将您的 Android Studio 指向新的 sdk 工具。

This fixed the error for me.

这为我修复了错误。

回答by slellis

This problem arise after I installed the Android SDK Build-tools version 21.1.2. So I went back to the 21.1.1 version and the problem was solved.

在我安装了 Android SDK Build-tools 版本 21.1.2 后出现了这个问题。于是我又回到了21.1.1版本,问题解决了。

回答by Mushir

Try (Build -> Rebuild project and then Build-> Clean project) if this not work try below procedure

尝试(构建 -> 重建项目,然后构建 -> 清理项目)如果这不起作用,请尝试以下过程

Please Click on File -> Invalidate Caches/Restart.. tab This will resolve this

请单击文件 -> 使缓存无效/重新启动.. 选项卡这将解决此问题

回答by smoothumut

Just clean the project. It worked for me.

只需清理项目。它对我有用。