Android - activity_main不能被解决,或者不是一个字段。

时间:2021-10-03 23:59:19

Previous people have had the same error message, but the solution has always been to remove or modify some import "android.R". I have no such import so I'm really lost

以前的人也有相同的错误消息,但是解决方案一直是删除或修改一些import“android.R”。我没有这样的东西,所以我真的迷路了。

I'm trying to get a sample android google maps program running.

我正在尝试使用一个android谷歌地图程序运行。

I'm following this tutorial. http://www.vogella.com/articles/AndroidGoogleMaps/article.html

我跟随本教程。http://www.vogella.com/articles/AndroidGoogleMaps/article.html

However, eclipse gives me this error when I paste over the code: "activity_main cannot be resolved or is not a field"

但是,当我在代码上粘贴“activity_main无法被解析或不是一个字段”时,eclipse会给我这个错误。

This happens in MainActivity in this snippet of code

这在代码片段的MainActivity中发生。

  public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;}

The specific error is in the "R.menu.activity_main" part.

具体错误在“r”菜单中。activity_main”部分。

Here is my activity_main.xml file.

这是我的activity_main。xml文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout> 

And here is my full MainActivity.java class

这是我的全部主要活动。java类

package com.example.mapssample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();
    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
    .title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
    .position(KIEL)
    .title("Kiel")
    .snippet("Kiel is cool")
    .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.ic_launcher)));

// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
  }

} 

Thank you in advance for all your help. Sorry for some wonky indenting in my code, I kept giving four extra spaces to make it appear as code and it got a little weird.

预先感谢你的帮助。抱歉,在我的代码中出现了一些不正常的缩进,我保留了四个额外的空格,让它看起来像代码一样,有点奇怪。

New, error log.

新的错误日志。

Thank you so much, do you have any idea what this means?

非常感谢,你知道这意味着什么吗?

06-07 22:45:25.226: E/AndroidRuntime(29901): FATAL EXCEPTION: main
06-07 22:45:25.226: E/AndroidRuntime(29901): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.Activity.onCreateView(Activity.java:4713)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.Activity.setContentView(Activity.java:1893)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.example.mapssample.MainActivity.onCreate(MainActivity.java:23)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.Activity.performCreate(Activity.java:5058)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.os.Looper.loop(Looper.java:137)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at android.app.ActivityThread.main(ActivityThread.java:5059)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at java.lang.reflect.Method.invokeNative(Native Method)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at java.lang.reflect.Method.invoke(Method.java:511)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
06-07 22:45:25.226: E/AndroidRuntime(29901):    at dalvik.system.NativeStart.main(Native Method)

11 个解决方案

#1


16  

1) Where does your layout file exist? is it under layout folder then shouldn't it be

1)您的布局文件存在于哪里?它在布局文件夹下吗?

R.layout.activity_main

2) You still have to import your projects R file

你仍然需要导入你的项目R文件。

com.example.mapssample.R;

3) R file is automatically generated if there's no error in your code. Sometimes when you get can't resolve R file error means you have problem somewhere else in the project. Try clean project. It might help.

如果你的代码中没有错误,R文件会自动生成。有时候,当您无法解决R文件错误时,就意味着您在项目的其他地方遇到了问题。尝试清洁项目。它可能会有所帮助。

4) You don't really need menu for testing this project. Also the activity_main file you showed is already included in the project and that's in layout folder. Just comment the menu part and see.

你不需要菜单来测试这个项目。您所展示的activity_main文件已经包含在项目中,这是在layout文件夹中。只要评论一下菜单部分就可以了。

#2


19  

Your Code imported android.R so first remove this line import android.R;

进口android代码。所以首先删除这条线导入android.R;

so remove this line & press cntrl+shift+o (to import necessary packages)..

因此,删除这一行并按cntrl+shift+o(导入必要的包)。

#3


6  

I had same error im my app and i solved it by 1.removing import android.R; and 2.import com.example.yourpakagename.R;.3. After that clean the project.4. run project

我有相同的错误,我的应用程序,我解决了它1。取消进口android.R;和2。进口com.example.yourpakagename.R;。3。然后把项目清理干净。运行项目

#4


1  

I have three ways that help me to solve this problem, may be it works for you.

我有三种方法来帮助我解决这个问题,也许对你有用。

  1. first clean than try to import your project like this: import com.example.mapssample.R;
  2. 首先,不要试图像这样导入您的项目:导入com.example.mapssample.R;
  3. right click on project go to properties--> android --> select target.
  4. 右键点击项目到属性-> android ->选择目标。
  5. clean
  6. 清洁

#5


1  

when you faced a red wave line under the Capital "R" just hold mouse there and Select second Line "import your_package_name.R" Because it link all related files of your project

当您在大写的“R”下面对一个红色的波浪线时,在那里按住鼠标并选择第二行“导入your_package_name”。因为它链接了你项目的所有相关文件。

Don't select first fix that says "import android.R"

不要选择“导入android.R”的第一个补丁

and it still shows error under activity_main, It means can't find those files in your project,check that file names like activity_main.xml etc..

它仍然显示在activity_main项下的错误,它意味着在您的项目中无法找到这些文件,检查activity_main这样的文件名。xml等。

Hope it Works..

希望它的工作原理。

#6


0  

the activity_main.xml file maybe is wrong.I mean there is some weird symbol error inside the file.you shold check it out.

activity_main。xml文件可能是错误的。我的意思是文件里面有一些奇怪的符号错误。你去看看吧。

#7


0  

I am not sure if this will solve your problem, but I have always declared fragment's type in xml with the "name" attribute, not the "class" attribute. Try making your fragment

我不确定这是否能解决您的问题,但我总是用“name”属性,而不是“class”属性来声明片段的xml类型。试着让你的片段

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment" />

#8


0  

Heh.. similar prob.. turns out somewhere along the line I had renamed the map fragment ID in the XML layout file.. Was looking for a fragment ID that did not exists.. hope it helps someone else.

哈. .遇到类似的问题。结果,我在XML布局文件中重新命名了map片段ID。正在寻找一个不存在的片段ID。希望它能帮助别人。

#9


0  

I'm following the Deitel Android, How to program-book, and when I tried building my first own application I experienced similar problems. What I did:

我正在跟踪Deitel Android,如何编程,当我尝试构建自己的第一个应用程序时,我遇到了类似的问题。我所做的:

In windows environment variables: HOME=%USERPROFILE% JAVA_HOME=\Program Files\Java\jre7\bin (or what/where you have it)

在windows环境变量中:HOME=%USERPROFILE% JAVA_HOME=\程序文件\Java\jre7\bin(或您拥有它的地方)

In the code: Mainactivity.java: Added Import com.deitel.welcome.R (that is package.R) Changed the call R.layout.activity_main to R.layout.main (check the generated R.java file)

在代码:Mainactivity。java:添加了Import com.deitel.welcome.R(这是package.R)更改了调用R.layout。activity_main出来。主(检查生成的R。java文件)

Working!

工作!

#10


0  

this is a common problem in eclipse. so 90% it is the problem with R.java file. so do following steps it will work for shore

这是eclipse中常见的问题。所以90%是R的问题。java文件。因此,遵循它将为shore工作的步骤。

1.Remove your import android.R

1。删除导入android.R

2.Ctrl+Shift+O.

2. ctrl + Shift + O。

3.clean the project

3所示。清洁项目

#11


-1  

Try the following

试试以下

com.example.mapssample.R.layout.activity_main

#1


16  

1) Where does your layout file exist? is it under layout folder then shouldn't it be

1)您的布局文件存在于哪里?它在布局文件夹下吗?

R.layout.activity_main

2) You still have to import your projects R file

你仍然需要导入你的项目R文件。

com.example.mapssample.R;

3) R file is automatically generated if there's no error in your code. Sometimes when you get can't resolve R file error means you have problem somewhere else in the project. Try clean project. It might help.

如果你的代码中没有错误,R文件会自动生成。有时候,当您无法解决R文件错误时,就意味着您在项目的其他地方遇到了问题。尝试清洁项目。它可能会有所帮助。

4) You don't really need menu for testing this project. Also the activity_main file you showed is already included in the project and that's in layout folder. Just comment the menu part and see.

你不需要菜单来测试这个项目。您所展示的activity_main文件已经包含在项目中,这是在layout文件夹中。只要评论一下菜单部分就可以了。

#2


19  

Your Code imported android.R so first remove this line import android.R;

进口android代码。所以首先删除这条线导入android.R;

so remove this line & press cntrl+shift+o (to import necessary packages)..

因此,删除这一行并按cntrl+shift+o(导入必要的包)。

#3


6  

I had same error im my app and i solved it by 1.removing import android.R; and 2.import com.example.yourpakagename.R;.3. After that clean the project.4. run project

我有相同的错误,我的应用程序,我解决了它1。取消进口android.R;和2。进口com.example.yourpakagename.R;。3。然后把项目清理干净。运行项目

#4


1  

I have three ways that help me to solve this problem, may be it works for you.

我有三种方法来帮助我解决这个问题,也许对你有用。

  1. first clean than try to import your project like this: import com.example.mapssample.R;
  2. 首先,不要试图像这样导入您的项目:导入com.example.mapssample.R;
  3. right click on project go to properties--> android --> select target.
  4. 右键点击项目到属性-> android ->选择目标。
  5. clean
  6. 清洁

#5


1  

when you faced a red wave line under the Capital "R" just hold mouse there and Select second Line "import your_package_name.R" Because it link all related files of your project

当您在大写的“R”下面对一个红色的波浪线时,在那里按住鼠标并选择第二行“导入your_package_name”。因为它链接了你项目的所有相关文件。

Don't select first fix that says "import android.R"

不要选择“导入android.R”的第一个补丁

and it still shows error under activity_main, It means can't find those files in your project,check that file names like activity_main.xml etc..

它仍然显示在activity_main项下的错误,它意味着在您的项目中无法找到这些文件,检查activity_main这样的文件名。xml等。

Hope it Works..

希望它的工作原理。

#6


0  

the activity_main.xml file maybe is wrong.I mean there is some weird symbol error inside the file.you shold check it out.

activity_main。xml文件可能是错误的。我的意思是文件里面有一些奇怪的符号错误。你去看看吧。

#7


0  

I am not sure if this will solve your problem, but I have always declared fragment's type in xml with the "name" attribute, not the "class" attribute. Try making your fragment

我不确定这是否能解决您的问题,但我总是用“name”属性,而不是“class”属性来声明片段的xml类型。试着让你的片段

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment" />

#8


0  

Heh.. similar prob.. turns out somewhere along the line I had renamed the map fragment ID in the XML layout file.. Was looking for a fragment ID that did not exists.. hope it helps someone else.

哈. .遇到类似的问题。结果,我在XML布局文件中重新命名了map片段ID。正在寻找一个不存在的片段ID。希望它能帮助别人。

#9


0  

I'm following the Deitel Android, How to program-book, and when I tried building my first own application I experienced similar problems. What I did:

我正在跟踪Deitel Android,如何编程,当我尝试构建自己的第一个应用程序时,我遇到了类似的问题。我所做的:

In windows environment variables: HOME=%USERPROFILE% JAVA_HOME=\Program Files\Java\jre7\bin (or what/where you have it)

在windows环境变量中:HOME=%USERPROFILE% JAVA_HOME=\程序文件\Java\jre7\bin(或您拥有它的地方)

In the code: Mainactivity.java: Added Import com.deitel.welcome.R (that is package.R) Changed the call R.layout.activity_main to R.layout.main (check the generated R.java file)

在代码:Mainactivity。java:添加了Import com.deitel.welcome.R(这是package.R)更改了调用R.layout。activity_main出来。主(检查生成的R。java文件)

Working!

工作!

#10


0  

this is a common problem in eclipse. so 90% it is the problem with R.java file. so do following steps it will work for shore

这是eclipse中常见的问题。所以90%是R的问题。java文件。因此,遵循它将为shore工作的步骤。

1.Remove your import android.R

1。删除导入android.R

2.Ctrl+Shift+O.

2. ctrl + Shift + O。

3.clean the project

3所示。清洁项目

#11


-1  

Try the following

试试以下

com.example.mapssample.R.layout.activity_main