gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:48:34 25 4
gpt4 key购买 nike

以前的人也有过同样的错误信息,但是解决办法一直是去掉或者修改一些import "android.R"。我没有这样的导入所以我真的迷路了

我正在尝试让示例 android google map 程序运行。

我正在学习本教程。 http://www.vogella.com/articles/AndroidGoogleMaps/article.html

但是,当我粘贴代码时,eclipse 给我这个错误:“activity_main cannot be resolved or is not a field”

这发生在这段代码的 MainActivity 中

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

具体错误在“R.menu.activity_main”部分。

这是我的 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>

这是我完整的 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;
}

}

预先感谢您的所有帮助。抱歉,我的代码中出现了一些不稳定的缩进,我一直在给它额外的四个空格以使其显示为代码,这有点奇怪。

新的,错误日志。

非常感谢,你知道这是什么意思吗?

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)

最佳答案

你的代码导入 android.R 所以首先删除这一行导入android.R;

所以删除这一行并按 ctrl+shift+o(导入必要的包)..

关于java - Android - activity_main 无法解析或不是字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16995402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com