【问题标题】:Why is my layout unable to be resolved? (Android/Java)为什么我的布局无法解决? (安卓/Java)
【发布时间】:2012-11-05 01:53:03
【问题描述】:

嗯,到目前为止,我的程序一直在工作,保持可编译性和一切,我试图向它添加声音,所以我创建了一个文件夹“raw”,并停留在 mp3 文件中。然后我尝试使用它,无论出于何种原因,一切都开始出现错误。我 Ctrl+Z'd 直到它回到我尝试添加声音之前。程序仍然出错。我删除了原始文件夹,仍然有错误[在下面的程序中标记] 请注意,这只是我程序中的 1 个类,还有其他类。

package scouting.form;

import android.R;//Don't use android.R here; use a fully qualified name for each usage instead - this one only comes up *sometimes...*
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity {

    Button login;
    EditText user;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);//Error: activity_login cannot be resolved or is not a field
        login=(Button) findViewById(R.id.button1);//NO ERROR even though the next line has an error...
        user=(EditText) findViewById(R.id.editText1);//Error: editText1 cannot be resolved or is not a field
        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent openScouting = new Intent("com.cody.graham.SCOUTING");
                startActivity(openScouting);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_login, menu);//Error: activity_login cannot be resolved or is not a field
        return true;
    }
}

我知道布局和菜​​单仍然存在,它们一直存在,我可以打开布局并确认布局文件夹中的“activity_login”布局中仍有一个编辑文本框,并且编辑文本框确实有这一行

android:id="@+id/editText1"

在里面。

【问题讨论】:

    标签: android layout resolve


    【解决方案1】:

    看到顶部了吗?

    import android.R;//Don't use android.R here; use a fully qualified name for each usage instead - this one only comes up *sometimes...
    

    您可能应该注意该评论所说的内容。 ;) 删除android.R 的导入,它将使用您项目的R 文件,这是activity_login 所在的位置。

    【讨论】:

    • 我不知道该说什么...我确实有一次删除了它,它抱怨我没有我需要的导入,所以我重新添加了它。这次当我删除它时,它起作用了......(facepalm?)无论如何,谢谢!
    • @bluebl1 不客气!不要忘记通过勾选左侧的复选标记将答案标记为正确。 ;)
    • 我又遇到了我所说的问题。我现在没有“import.android.R”,但我有这一行:'clr=(Button) findViewById(R.id.CLREntries);'并且它说它无法解析“CLREntries” 在此页面的布局中,我有一个带有此行的按钮:'android:id="@+id/CLREntries"' 并且该按钮没有错误。
    • 尝试清理项目。如果这还不够,请将您的完整代码和 XML 发布在一个新问题中……正如您刚刚了解到的那样,在 cmets 中交换代码是……好吧……不可能的。
    【解决方案2】:

    您导入了错误的R.java。您应该导入由 Eclipse 生成的那个(在您的 gen 文件夹中)。您可以通过键入 control-shift-o 来自动导入它。您可能还需要运行 Project>Clean,这在添加资产时有时是需要的。

    至于raw文件夹,一定要放在res/目录下。

    【讨论】:

      猜你喜欢
      • 2013-08-11
      • 2019-06-11
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多