【问题标题】:R cannot be resolved to a variable & activity_main cannot be resolved or is not a field , xml is also ok but no file in gen folder [duplicate]R无法解析为变量&activity_main无法解析或不是字段,xml也可以,但gen文件夹中没有文件[重复]
【发布时间】:2013-10-20 17:27:38
【问题描述】:

我是安卓应用开发的新手,

我刚刚创建了一个简单的android项目,但是当它创建时显示错误。

当我按下 control shift +o 时“R 无法解析为变量”

android.R 自动添加到我的代码中,但现在它显示此错误

“activity_main 无法解析或不是字段”

这是这个项目的代码

package com.php.helloworld;

import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;


public class MainActivity extends Activity {

    @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.main, menu);
        return true;
    }

}

当我试图写下这一行时

导入 com.php.helloworld.R 它显示“无法解析导入 com.php.helloworld.R”

我的xml文件也没有问题,gen文件夹中也没有文件 有人可以帮我解决这个问题

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:

    删除导入android.R。它实际上是指 android 默认库中的 android 资源。如果你想使用你的资源,你必须导入你的 R 文件而不是 android 的默认库。检查项目的 gen 文件夹中是否存在 R.java。

    【讨论】:

    • gen 文件夹中没有文件,我的 xml 很好,因为我没有更改任何内容,我没有写任何内容,我刚刚创建了这只是简单的 hello world
    • 它已经解决了,我在上面添加了android构建工具,然后清理项目错误一直在发生,谢谢naresh
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2019-08-13
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多