[英]Eclipse for android: Issues with R
我正在尝试做一个定位应用程序,现在正处于故障排除阶段。 我归结为最后两个问题,它们都与 R. blah blah 有关。 它说 main 要么不是字段,要么无法解析,并且显示相同的消息,但使用 mapview 而不是 main。 以下是我的导入和问题所在的代码行。
代码:
setContentView(R.layout.main);
mapview = (MapView)findViewById(R.id.mapview);
进口:
import android.R;
import android.R.layout;
import android.R.id;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
我什么都试过了。 我知道人们一直说要删除 android.R 但这就是在 jar 文件中找到 R.layout 和 R.id 的地方。 我已右键单击该文件夹并验证了代码。 我已经去了 Project/Clean 并做到了。 没运气。 请帮忙? 这是我的第一篇文章,所以如果格式错误,我很抱歉。 我预先感谢您为我提供的任何帮助。 这是我的 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Waiting for location..."
android:id="@+id/lblLocationInfo"
/>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0vFrUOhHMkbahT9zXqiz_DuNVWfPqlEyqcO8ftg"
/>
</LinearLayout>
尝试删除该行:
import android.R;
然后做Build Clean,这可以解决问题。
除非您使用来自 android 的资源,否则您不需要这些行
import android.R;
import android.R.Id;
import android.R.layout;
删除那些行
R 正在解析为 androids R 而不是com.yourpackage.yourproject.R
。
如果不需要,请删除导入。 如果您需要在代码中明确表示。 前任。
ImageView.setDrawable(android.R.ic_menu_search); //uses an android resource
findViewById(R.id.mapView); //uses your resource file
setContentView(R.layout.activity_main); // must have an activity_main.xml file in YourProject/res/layout
编辑:
更改以下代码:
//import android.R;
//import android.R.layout; //comment or delete these lines
//import android.R.id;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
setContentView(R.layout.activity_main); //changed main to activity_main
mapview = (MapView)findViewById(R.id.mapview);
编辑 2
在 MainActivity.java 的最顶部,它应该有一行以package
开头,该行的其余部分是什么意思?
在您的 AndroidManifest.xml 中还有一个 package 条目,它说的是什么?
R.java
它是一个自生成的文件,您需要重新构建它才能生成它,忽略警告并“构建清理”您的文件
这也可能与您的项目设置有关。 创建新项目时,我通常会设置“订购和导出”选项。 这样您就不必导入任何 R.* 包。
在 Eclipse 中,右键单击您的项目,转到Properties -> Java Build Path -> Order and Export
。 只需确保 yourprojectname/gen 文件夹位于 yourprojectname/src 文件夹上方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.