Android Google Map api v2会返回null吗?

时间:2013-06-04 14:20:33

标签: android google-maps-api-2

我有下面的代码和它的工作好的tvTitle没有返回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);
                               ^^^^^^  

但谷歌地图返回null ...请查看以下代码...

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>

1 个答案:

答案 0 :(得分:4)

取自SupportMapFragment documentation

  

只有在加载基础地图系统并且片段中的基础视图存在时,才能使用getMap()获取GoogleMap。该类自动初始化地图系统和视图;但是,当它准备就绪时,您无法保证,因为这取决于Google Play服务APK的可用性。如果GoogleMap不可用,则getMap()将返回null。

然后,在您的活动中,您可以使用GooglePlayServicesUtil检查可用性:

// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

// Check if we were successful in obtaining the map.
if (mMap != null) {
    // Setup your map...
} else {
    int isEnabled = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (isEnabled != ConnectionResult.SUCCESS) {
        GooglePlayServicesUtil.getErrorDialog(isEnabled, this, 0);
    }
}

Here in method setUpMapIfNeeded您有一个我的应用程序的实例:Tureame