findViewById 对于布局中的视图返回 null
我的 MapView 有一些奇怪的问题。
<?xml version="1.0" encoding="utf-8"?>
<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="my-api-key"/>
并且 mapView = (MapView)findViewById(R.id.mapview);
为我返回 null 。但MapView显示正常。我做错了什么?
UPD:这是我的班级
public class WayMapActivity extends MapActivity {
private MapView mapView = null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.maplayout);
mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
@Override
public void onResume(){
super.onResume();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
I have some strange problem, with the MapView.
<?xml version="1.0" encoding="utf-8"?>
<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="my-api-key"/>
And mapView = (MapView)findViewById(R.id.mapview);
return null for me. But MapView displayed normaly. What i'm doing wrong?
UPD: There is my class
public class WayMapActivity extends MapActivity {
private MapView mapView = null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.maplayout);
mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
@Override
public void onResume(){
super.onResume();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否在
findViewById
之前调用setContentView(layout)
?然后尝试清理并重建您的项目。有时需要重建R
Do you call
setContentView(layout)
beforefindViewById
?Then try to clean and rebuild your project. Sometimes it is necessary to rebuild R
这种情况时常发生在我身上。
This happens to me from time to time.