【问题标题】:findViewById return null for view from layoutfindViewById 为布局中的视图返回 null
【发布时间】:2011-09-01 22:26:20
【问题描述】:

我遇到了一些奇怪的问题,就是 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;
}
  }

【问题讨论】:

  • 调用该行之后 setContentView()?
  • 有时通过清理项目来解决
  • 另外,您的课程是否扩展了 MapActivity?
  • 发布一些你的 Java 源代码。

标签: android android-mapview


【解决方案1】:

你在findViewById之前打电话给setContentView(layout)吗?

然后尝试清理并重建您的项目。有时需要重建R

【讨论】:

  • 尝试清理并重建您的项目。有时需要重建 R
  • 谢谢。清理项目对我有帮助。
  • 我很高兴它有帮助。这样的问题会浪费很多时间并导致脑损伤:)
【解决方案2】:

这种情况时常发生在我身上。

  1. 删除 gen 文件夹
  2. 清理项目
  3. 重新运行

【讨论】:

    猜你喜欢
    • 2013-03-20
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多