Android 为什么版面上的项目可以';你不能在课堂上使用吗?
我正在尝试一个在internet上获得的项目,但当我在EclipseJuno上导入它时,activity_main.xml中的项目无法使用。 下面是activity_main.xml代码`Android 为什么版面上的项目可以';你不能在课堂上使用吗?,android,google-maps,Android,Google Maps,我正在尝试一个在internet上获得的项目,但当我在EclipseJuno上导入它时,activity_main.xml中的项目无法使用。 下面是activity_main.xml代码` <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > &l
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_btn_find"
android:layout_alignParentRight="true" />
<EditText
android:id="@+id/et_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hnt_et_location"
android:layout_toLeftOf="@id/btn_find" />
</RelativeLayout>
<com.google.android.maps.MapView
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:apiKey="02XkocQETlvu36mzmDnwYr-A4GTX5mENmESOG8A"
android:clickable="true" />
`
下面是MainActivity.java的代码
`
import java.io.IOException;
导入java.util.List;
导入android.R;
导入android.graphics.drawable.drawable;
导入android.location.Address;
导入android.location.Geocoder;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapController;
导入com.google.android.maps.MapView;
导入com.google.android.maps.Overlay;
导入com.google.android.maps.OverlayItem;
公共类MainActivity扩展了MapActivity{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取对布局活动\u main的btn\u查找的引用
按钮btn\u find=(按钮)findviewbyd(R.id.btn\u find);
//为“查找”按钮定义按钮单击事件侦听器
OnClickListener findClickListener=新建OnClickListener(){
@凌驾
公共void onClick(视图v){
//获取对EditText的引用以获取用户输入位置
EditText etLocation=(EditText)findViewById(R.id.et_位置);
//获取用户输入位置
字符串位置=etLocation.getText().toString();
if(location!=null&&!location.equals(“”){
新的GeocoderTask().execute(位置);
}
}
};
//设置按钮单击“查找”按钮的事件侦听器
btn_find.setOnClickListener(findClickListener);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
@凌驾
受保护的布尔值isRouteDisplayed(){
//TODO自动生成的方法存根
返回false;
}
//用于访问地理编码Web服务的AsyncTask类
私有类GeocoderTask扩展异步任务{
@凌驾
受保护列表doInBackground(字符串…位置名称){
//创建Geocoder类的实例
Geocoder Geocoder=新的Geocoder(getBaseContext());
列表地址=空;
试一试{
//获取最多3个与输入文本匹配的地址
addresses=geocoder.getFromLocationName(locationName[0],3);
}捕获(IOE异常){
e、 printStackTrace();
}
返回地址;
}
@凌驾
受保护的void onPostExecute(列表地址){
//获取对布局活动\u main的MapView的引用
MapView MapView=(MapView)findViewById(R.id);
//设置缩放控件
mapView.SetBuilTinZoomControl(真);
//获取MapView的MapController
MapController mc=mapView.getController();
//获取与资源映像对应的可绘制对象
Drawable Drawable=getResources().getDrawable(R.Drawable.marker);
//获取地图的覆盖图
List overlays=mapView.getOverlays();
//创建ItemizedOverlay
LocationOverlay LocationOverlay=新的LocationOverlay(可绘制,getBaseContext());
//清除覆盖物
覆盖。清除();
if(addresses==null | | addresses.size()==0){
Toast.makeText(getBaseContext(),“找不到位置”,Toast.LENGTH_SHORT.show();
//重新绘制地图以清除覆盖
mapView.invalidate();
}
//在谷歌地图上为每个匹配地址添加标记
对于(int i=0;i 0?地址。getAddressLine(0):“”,
address.getCountryName());
//创建覆盖项以标记点
OverlayItem OverlayItem=新的OverlayItem(p,“位置”,地址文本);
//在LocationOverlay中添加OverlayItem
locationOverlay.addOverlay(overlayItem);
//将locationOverlay添加到覆盖中
叠加。添加(locationOverlay);
//找到第一个位置
如果(i==0)
司马迁(p),;
}
//重新绘制地图
mapView.invalidate();
}
}
}`
该错误在“R.layout.activity\u main”、“Button btn\u find=(Button)findviewbyd(R.id.btn\u find)”等行中显示错误。活动_main和btn_find以及另一个标记为错误的R.id.x。
有什么问题?谢谢。您需要做的第一件事是OnClickListener的类应该如下:-
public void onCreate(Bundle savedInstanceState){
Button btn_find = (Button) findViewById(R.id.btn_find);
btn_find.setOnClickListener(findClickListener);
}
OnClickListener findClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// Getting reference to EditText to get the user input location
EditText etLocation = (EditText) findViewById(R.id.et_location);
// Getting user input location
String location = etLocation.getText().toString();
if(location!=null && !location.equals("")){
new GeocoderTask().execute(location);
}
}
};
看看这个,并尝试清理您的项目
按如下方式使用布局:-
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_btn_find"
android:layout_alignParentRight="true" />
<EditText
android:id="@+id/et_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hnt_et_location"
android:layout_toLeftOf="@id/btn_find" />
<com.google.android.maps.MapView
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:apiKey="02XkocQETlvu36mzmDnwYr-A4GTX5mENmESOG8A"
android:clickable="true" />
</RelativeLayout>
看看吧 添加这个xmlns:android=”http://schemas.android.com/apk/res/android“哦,好吧,我发现了我的问题。当我创建新项目时,我的项目上没有R.java文件。如何解决它?只要清理你的项目,你可以探索你的包资源管理器的内容显示什么。。。!!!!你是在使用搜索优化还是在谷歌地图上点击按钮搜索地点…??是的,我创建了一个在谷歌上搜索地点的项目。但我不知道为什么R.id.activity\u主项上会出现错误。这是否意味着没有活动主布局?但是我检查了R.java,它列出了布局活动和布局上的所有项目。只需清理您的项目并观看它,您可以在这里发布您正在观察的logcat错误。。。!!!希望这个链接能帮助你。。。!!!我已经加了,但还是不起作用。。我也用xmlns:tools=“完成了它。但仍然显示
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_btn_find"
android:layout_alignParentRight="true" />
<EditText
android:id="@+id/et_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hnt_et_location"
android:layout_toLeftOf="@id/btn_find" />
<com.google.android.maps.MapView
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:apiKey="02XkocQETlvu36mzmDnwYr-A4GTX5mENmESOG8A"
android:clickable="true" />
</RelativeLayout>