“无法解析符号R”,但应用程序构建没有错误

时间:2018-10-27 14:24:41

标签: java android

我遇到的问题与其他人在 SO 上遇到的问题相同。在我的项目中,android studio说它无法解析符号R。

奇怪的是,它构建良好。我的问题类似于 SO 上的this帖子。

他的回答中唯一提到的是与他人合作时可能会发生此问题,并且由于错误我不得不从GitHub下载项目的主副本。

我在我的项目中尝试了以下操作:

清洁项目 重建项目。 重命名所有带有拼写错误的文件(helloworld为hello_world) 将项目名称从MyApp-Master更改为原始名称MyApp。 运行android lint以查找代码语法中的任何错误并进行修复 使用值代替硬编码属性

我还整理了{strong> SO 帖子herehere,但不幸的是,这些建议没有帮助。

我正在使用android studio 3.2.1

我没有包括所有无法解析R的类,因为R属于所有此类。 但这是 MainActivity.class

except KeyError: pass

这是相关的 activity_main.xml

   package com.example.cohen.theepiccalculator;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {


     private DrawerLayout drawerLayout;
     private int drawerId;


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

        Toolbar toolbar = findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        if (actionBar != null) {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_button_drop_down);
        }


        drawerLayout = findViewById(R.id.drawer_layout);


        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                drawerId = menuItem.getItemId();
                menuItem.setChecked(true);
                selectItem(drawerId);
                drawerLayout.closeDrawers();
                return true;
            }
        });


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void selectItem(int position){


        switch (position){
            case R.id.nav_currency :
                Intent intentC = new Intent(MainActivity.this, InputActivityCurrency.class);
                startActivity(intentC);
                Toast.makeText(MainActivity.this,"Currency", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_length :
                Intent intentL = new Intent(MainActivity.this, InputActivityLength.class);
                startActivity(intentL);
                Toast.makeText(MainActivity.this,"Length", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_weight :
                Intent intentW = new Intent(MainActivity.this, InputActivityWeight.class);
                startActivity(intentW);
                Toast.makeText(MainActivity.this,"Weight", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_temp :
                Intent intentT = new Intent(MainActivity.this, InputActivityTemperature.class);
                startActivity(intentT);
                Toast.makeText(MainActivity.this,"Temp", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_area :
                Intent intentA = new Intent(MainActivity.this, InputActivityArea.class);
                startActivity(intentA);
                Toast.makeText(MainActivity.this,"Area", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_speed :
                Intent intentS = new Intent(MainActivity.this, InputActivitySpeed.class);
                startActivity(intentS);
                Toast.makeText(MainActivity.this,"Speed", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_vol :
                Intent intentV = new Intent(MainActivity.this, InputActivityVolume.class);
                startActivity(intentV);
                Toast.makeText(MainActivity.this,"Vol", Toast.LENGTH_SHORT).show();
                break;

        }


    }
}

如果我遗漏了一些可能有用的东西,请告诉我。

1 个答案:

答案 0 :(得分:0)

当R失败时,通常错误是在xml中或可能找不到菜单,图像等资源。 您可以执行build>重建项目并附加错误。

祝你好运

相关问题