Android Studio:无法解析符号"R"

Nic*_*las 7 android r.java-file

我知道这个问题已被问过几次,但由于我的背景,没有答案是有用的.问题是,我刚刚打开了我的第一个项目(在我的实际计算机中)而没有编辑所有的"R".在mainActivity.java中变成红色.有谁知道这个问题的原因是什么以及如何解决?

这是.java主要活动

package com.nico.calc_02;
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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是.xml布局:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我没有修改代码或任何东西.我正在使用Android Studio.有谁知道如何修理它?

Bla*_*rai 8

有几件事情可能是:

1. You may need to do a clean & build
2. You may need to update the SDK (Go into your SDK manager, and make sure that all your files are up to date)
Run Code Online (Sandbox Code Playgroud)

我发现这种情况主要发生在我更新AS时,当您进行AS更新时,您还需要通过SDK管理器更新SDK.确保您的工具也是最新的,而不仅仅是API.喜欢的东西com.android.support:support,并com.google.android.gms:play-services会导致此错误.检查Run左下方的选项卡,因为这样可以让您了解某些内容是否已过期,并且您需要更新.如果您使用的是库,那么它们也可能已经更新.

您还需要检查目录中的build.gradle文件mobile,并确保通过SDK管理器将依赖项与SDK内联.这通常是我的罪魁祸首,特别是当我更新AS时.

不幸的是,没有"简单"的解决方案.在找到它之前,你只需要尝试不同的东西,因为它可以像做"清理和构建"一样简单.

祝好运!