2013-06-04 5 views
2

У меня есть ниже код и его работает хорошо tvTitle не возвращается нуль, ноAndroid Google Map api v2 возвращает null?

RelativeLayout relmain = (RelativeLayout) findViewById(R.id.mainview); 
LayoutInflater inflater = LayoutInflater.from(this); 

RelativeLayout contents = (RelativeLayout) inflater 
      .inflate(R.layout.placedetail, null); 
relmain.addView(contents); 

TextView tvTitle = (TextView) contents.findViewById(R.id.tvdealtitle); 
           ^^^^^^ 

но Google карта вернуть мне пустой ... проверьте ниже код ...

GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map2)).getMap(); 


System.out.println("Google map is :: " + googleMap); 

placedetail.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/bgmap" 
android:orientation="vertical" > 

<RelativeLayout 
    android:id="@+id/rel1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/banner" 
    android:onClick="onClickBack" > 

    <ImageView 
     android:id="@+id/img" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="5dp" 
     android:background="@drawable/directions" 
     android:onClick="onClickImage" /> 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/reldetail" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/rel1" 
    android:layout_marginTop="10dp" > 

    <TextView 
     android:id="@+id/tvdealtitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="8dp" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:text="Title" 
     android:textColor="@android:color/black" 
     android:textSize="16sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/tvdealoffertext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/tvdealtitle" 
     android:layout_below="@+id/tvdealtitle" 
     android:layout_marginTop="2dp" 
     android:ellipsize="end" 
     android:maxLines="2" 
     android:text="offertext" 
     android:textColor="#696969" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/tvdealdesc" 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" 
     android:layout_alignLeft="@+id/tvdealtitle" 
     android:layout_below="@+id/tvdealoffertext" 
     android:layout_marginTop="2dp" 
     android:scrollbars="vertical" 
     android:text="desc" 
     android:textColor="#696969" 
     android:textStyle="bold" /> 
</RelativeLayout> 

<fragment 
    android:id="@+id/map2" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/reldetail" /> 

</RelativeLayout> 
+0

Возможно, вы слишком быстро вызываете 'getMap()', возможно, до того, как фрагмент был вызван с 'onCreateView()'. – CommonsWare

+0

i call gmap initialize after oncreate завершение конца oncreate.i хотите показать mapview и выше mapview есть три textview, но google map получает нуль, покажите, как я могу получить googlemap с помощью 'contents.findViewById', показать выше код' tvTitle' is не получает нуль, но googlemap получает нуль ... –

+0

Я имею в виду 'GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager(). content. .findFragmentById (R.id.map2)). getMap(); 'но его дать мне ошибку –

ответ

4

Взятые из SupportMapFragment documentation:

GoogleMap может быть получен только с использованием getMap(), когда загружена базовая система карт и существует основной вид фрагмента. Этот класс автоматически инициализирует систему карт и представление; однако вы не можете быть уверены, когда он будет готов, потому что это зависит от доступности APK сервисов Google Play. Если GoogleMap недоступен, getMap() вернет значение null.

Тогда в вашей деятельности вы можете использовать GooglePlayServicesUtil чтобы проверить о наличии:

Here in method setUpMapIfNeeded У вас есть рабочий пример одного из моих приложений: Tureame.

Смежные вопросы