简体   繁体   English

Android - activity_main 无法解析或不是字段

[英]Android - activity_main cannot be resolved or is not a field

Previous people have had the same error message, but the solution has always been to remove or modify some import "android.R".以前的人有同样的错误信息,但解决方案一直是删除或修改一些导入“android.R”。 I have no such import so I'm really lost我没有这样的进口所以我真的迷路了

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"但是,当我粘贴代码时,eclipse 给了我这个错误:“activity_main 无法解析或不是字段”

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.menu.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这是我完整的 MainActivity.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)

Your Code imported android.R so first remove this line import android.R;您的代码导入了 android.R,因此首先删除这一行 import android.R;

so remove this line & press cntrl+shift+o (to import necessary packages)..所以删除这一行并按 cntrl+shift+o (导入必要的包)..

1) Where does your layout file exist? 1)你的布局文件在哪里? is it under layout folder then shouldn't it be是在布局文件夹下,那么不应该是

R.layout.activity_main

2) You still have to import your projects R file 2) 你仍然需要导入你的项目 R 文件

com.example.mapssample.R;

3) R file is automatically generated if there's no error in your code. 3) 如果您的代码中没有错误,则会自动生成 R 文件。 Sometimes when you get can't resolve R file error means you have problem somewhere else in the project.有时当你得到无法解决 R 文件错误意味着你在项目的其他地方有问题。 Try clean project.尝试干净的项目。 It might help.它可能会有所帮助。

4) You don't really need menu for testing this project. 4) 你真的不需要菜单来测试这个项目。 Also the activity_main file you showed is already included in the project and that's in layout folder.此外,您显示的 activity_main 文件已经包含在项目中,并且位于 layout 文件夹中。 Just comment the menu part and see.只需评论菜单部分并查看。

I had same error im my app and i solved it by 1.removing import android.R;我在我的应用程序中遇到了同样的错误,我通过 1.removing import android.R;解决了它import android.R; and 2. import com.example.yourpakagename.R;和 2. import com.example.yourpakagename.R; .3. .3. After that clean the project.4.之后清理project.4。 run project运行项目

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;首先清理而不是尝试像这样导入您的项目: import com.example.mapssample.R;
  2. right click on project go to properties--> android --> select target.右键单击项目转到属性--> android--> 选择目标。
  3. clean干净的

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”下遇到红色波浪线时,只需将鼠标放在那里并选择第二行“import your_package_name.R”,因为它链接了您项目的所有相关文件

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..希望它有效..

activity_main.xml 文件可能是错误的。我的意思是文件中有一些奇怪的符号错误。你应该检查一下。

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.我不确定这是否能解决您的问题,但我一直在 xml 中使用“name”属性而不是“class”属性声明片段的类型。 Try making your fragment尝试制作您的片段

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

嘿.. 类似的问题.. 原来我在 XML 布局文件中重命名了地图片段 ID 的某个地方.. 正在寻找一个不存在的片段 ID.. 希望它可以帮助其他人。

I'm following the Deitel Android, How to program-book, and when I tried building my first own application I experienced similar problems.我正在关注 Deitel Android,How to program-book,当我尝试构建我自己的第一个应用程序时,我遇到了类似的问题。 What I did:我做了什么:

In windows environment variables: HOME=%USERPROFILE% JAVA_HOME=\\Program Files\\Java\\jre7\\bin (or what/where you have it)在 Windows 环境变量中: HOME=%USERPROFILE% JAVA_HOME=\\Program Files\\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.layout.main(查看生成的R.java文件)

Working!在职的!

this is a common problem in eclipse.这是eclipse中的一个常见问题。 so 90% it is the problem with R.java file.所以 90% 是 R.java 文件的问题。 so do following steps it will work for shore所以请按照以下步骤操作,它将适用于岸上

1.Remove your import android.R 1.删​​除你的导入android.R

2.Ctrl+Shift+O. 2.Ctrl+Shift+O。

3.clean the project 3.清理项目

尝试以下

com.example.mapssample.R.layout.activity_main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM