Android setContentView(R.layout.Main); Main 无法解析或不是字段

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

setContentView(R.layout.Main); Main cannot be resolved or is not a field

androidandroid-widgetjavadoc

提问by max

This is a simple program for beginning (simple for you, not for me) eclipse. I want to change a text by clicking on a button, but it's not working.

这是一个用于开始(对你来说很简单,对我来说很简单)eclipse 的简单程序。我想通过单击按钮来更改文本,但它不起作用。

Here is the code:

这是代码:

package com.example.androidcourse;  

import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
import android.R.layout;  

public class MainActivity extends Activity {  

@Override 
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.Main);  


Button btn = (Button) findViewById(R.id.but_action); 
final TextView text = (TextView) findViewById(R.id.txt_caption);  
btn.setOnClickListener(new OnClickListener() {  

@Override  
public void onClick(View v) {
text.setText("New bingo...");  
}  
});  
}  
}  

回答by ania

First just build your project. If it will not work, then delete from imports android.R.layout and use layout from your package.

首先只是构建你的项目。如果它不起作用,则从导入 android.R.layout 中删除并使用包中的布局。

回答by Sunny Kumar Aditya

setContentView(R.layout.Main);

DO you have layout named Main ? as far as I know Capital letter do not mix well with layout names. If you use it you will get some error like this

你有名为 Main 的布局吗?据我所知,大写字母与布局名称不能很好地混合。如果你使用它,你会得到一些这样的错误

File-based resource names must start with a lowercase letter.

基于文件的资源名称必须以小写字母开头。

Please check and rename your layout file .

请检查并重命名您的布局文件。

回答by Hyman7en

First check the name of your layout XML (YourProject/res/layout/your_main.xmlof your project folder).

首先检查布局 XML 的名称(YourProject/res/layout/your_main.xml项目文件夹的名称)。

replace:

代替:

setContentView(R.layout.**Main**);

with:

和:

setContentView(R.layout.**your_main**);   // 'your_main' Your layout xml file name.

Mine was activity_main.xml, which I replaced with "main".

我的是activity_main.xml,我用“ main”替换了它。

I am just a beginner. I may be wrong but this solved my problem.

我只是一个初学者。我可能错了,但这解决了我的问题。

回答by Sneha Bhalerao

just type: import android.R.layout; This solved my problem hope it solves your problem too.

只需输入:import android.R.layout; 这解决了我的问题,希望它也能解决您的问题。

回答by Saddam Hussain

Remove all imports and regenerate, by alt+ Enter on every class that require import , this worked for me.

删除所有导入并重新生成,在需要 import 的每个类上按 alt+ Enter ,这对我有用。