findViewById возвращает null для вида из макета - PullRequest
1 голос
/ 01 сентября 2011

У меня странная проблема с 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); верните мне ноль. Но 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;
}
  }

Ответы [ 2 ]

0 голосов
/ 01 сентября 2011

Это случается со мной время от времени.

  1. Удалить папку gen
  2. Сделай проект чистым
  3. Re-бег
0 голосов
/ 01 сентября 2011

Вы звоните setContentView(layout) до findViewById?

Затем попробуйте очистить и перестроить свой проект.Иногда необходимо перестроить R

...