main不能被解析,也不能是一个字段。

时间:2021-12-16 23:53:11

This error occurs in setContentView line in this code snippet:

此错误发生在此代码片段的setContentView行中:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

I understand that in order for R.layout.main to be resolved, a file named res/layout/main.xml must be present.

我理解为r布局。要解决的主要问题,一个名为res/layout/main的文件。xml必须存在。

It is present and valid (i.e. Eclipse marks it as perfect without any errors). So, what else could cause this error?

它是有效的(即Eclipse将其标记为完美无缺,没有任何错误)。那么,还有什么原因导致这个错误呢?

BTW, I already tried Project > Clean. The error persists.

顺便说一下,我已经试过了>项目的Clean。错误依然存在。

9 个解决方案

#1


139  

Make sure you don't have this in your imports:

确保在你的进口产品中没有这个:

import android.R;

but:

但是:

import your.application.packagename.R;

#2


10  

Also try: add

也尝试:添加

import your.application.packagename.R;

and run: eclipse->project->clean..

和运行:eclipse - >项目- >干净. .

The error should be gone.

错误应该消失了。

#3


2  

If this is the problem you are encountering then check at the top of your code - you will see:

如果这是您遇到的问题,然后在您的代码顶部检查—您将看到:

import android.R

Delete that line and and change it into

删除该行,并将其更改为。

import (com.xx.yy)

Replace (com.xx.yy) with your actual package name for the class. This problem occurs mainly when you copy all the XML and Java code of another application and paste it into your new application.

用您实际的包名替换类。这个问题主要发生在您复制其他应用程序的所有XML和Java代码并将其粘贴到您的新应用程序中时。

This is an example of how I replaced the android.R packages within one of my own apps (a puzzle game):

这是我如何替换android的一个例子。R包在我自己的一个应用程序(一个拼图游戏):

package com.pir.PUZZLEGAME_NEW;

import com.pir.PUZZLEGAME_NEW.*;
import com.pir.puzzlegame_old.R;

Good luck!

好运!

#4


2  

Delete all your import statements from the class where you are getting this error message.

从您获取此错误消息的类中删除所有导入语句。

Then press ctrl+shift+o

然后按ctrl + shift + o

#5


0  

Also, sometimes it still won't compile even after making that needed change; but if you go ahead and run the app, the errs will clear up.

而且,有时即使在做出必要的更改之后,它仍然无法编译;但是如果你继续运行这个应用程序,错误就会清除。

#6


0  

If you are using eclipse, then

如果您正在使用eclipse,那么。

  1. Delete all your import statements from the class where you are getting this error message.

    从您获取此错误消息的类中删除所有导入语句。

  2. press CTRL + SHIFT + O --> OR select Source from menu and select Organize import. This will import all the necessary classes.

    按CTRL + SHIFT + O ->或从菜单中选择Source,选择组织导入。这将导入所有必要的类。

Note: Since you have a local R.class file, it will import your local file instead of the android.R file.

注意:因为你有一个本地R。类文件,它将导入您的本地文件而不是android。R文件。

Hope this will help someone.

希望这能帮助到别人。

#7


0  

I have removed the following line and it worked for me.

我已经删除了下面一行,它对我起作用了。

import android.R;

进口android.R;

#8


0  

I changed

我改变了

   import android.R;

to

 import your.application.packagename.R;

And project ->clean still the error.

而project ->仍然保持错误。

But when I Run the project the error automatically gone, may be Eclipse problem.

但是当我运行这个项目时,错误会自动消失,可能是Eclipse的问题。

#9


0  

for me changing

为我改变

import org.opencv.R;

to

import com.example.opencvtryagain.R;

solved it, where as my package name was com.example.opencvtryagain;

解决了它,因为我的包名是com.example.opencvtryagain;

#1


139  

Make sure you don't have this in your imports:

确保在你的进口产品中没有这个:

import android.R;

but:

但是:

import your.application.packagename.R;

#2


10  

Also try: add

也尝试:添加

import your.application.packagename.R;

and run: eclipse->project->clean..

和运行:eclipse - >项目- >干净. .

The error should be gone.

错误应该消失了。

#3


2  

If this is the problem you are encountering then check at the top of your code - you will see:

如果这是您遇到的问题,然后在您的代码顶部检查—您将看到:

import android.R

Delete that line and and change it into

删除该行,并将其更改为。

import (com.xx.yy)

Replace (com.xx.yy) with your actual package name for the class. This problem occurs mainly when you copy all the XML and Java code of another application and paste it into your new application.

用您实际的包名替换类。这个问题主要发生在您复制其他应用程序的所有XML和Java代码并将其粘贴到您的新应用程序中时。

This is an example of how I replaced the android.R packages within one of my own apps (a puzzle game):

这是我如何替换android的一个例子。R包在我自己的一个应用程序(一个拼图游戏):

package com.pir.PUZZLEGAME_NEW;

import com.pir.PUZZLEGAME_NEW.*;
import com.pir.puzzlegame_old.R;

Good luck!

好运!

#4


2  

Delete all your import statements from the class where you are getting this error message.

从您获取此错误消息的类中删除所有导入语句。

Then press ctrl+shift+o

然后按ctrl + shift + o

#5


0  

Also, sometimes it still won't compile even after making that needed change; but if you go ahead and run the app, the errs will clear up.

而且,有时即使在做出必要的更改之后,它仍然无法编译;但是如果你继续运行这个应用程序,错误就会清除。

#6


0  

If you are using eclipse, then

如果您正在使用eclipse,那么。

  1. Delete all your import statements from the class where you are getting this error message.

    从您获取此错误消息的类中删除所有导入语句。

  2. press CTRL + SHIFT + O --> OR select Source from menu and select Organize import. This will import all the necessary classes.

    按CTRL + SHIFT + O ->或从菜单中选择Source,选择组织导入。这将导入所有必要的类。

Note: Since you have a local R.class file, it will import your local file instead of the android.R file.

注意:因为你有一个本地R。类文件,它将导入您的本地文件而不是android。R文件。

Hope this will help someone.

希望这能帮助到别人。

#7


0  

I have removed the following line and it worked for me.

我已经删除了下面一行,它对我起作用了。

import android.R;

进口android.R;

#8


0  

I changed

我改变了

   import android.R;

to

 import your.application.packagename.R;

And project ->clean still the error.

而project ->仍然保持错误。

But when I Run the project the error automatically gone, may be Eclipse problem.

但是当我运行这个项目时,错误会自动消失,可能是Eclipse的问题。

#9


0  

for me changing

为我改变

import org.opencv.R;

to

import com.example.opencvtryagain.R;

solved it, where as my package name was com.example.opencvtryagain;

解决了它,因为我的包名是com.example.opencvtryagain;