Android Google Map api v2 返回 null?

标签 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>

最佳答案

取自 SupportMapFragment documentation :

A GoogleMap can only be acquired using getMap() when the underlying maps system is loaded and the underlying view in the fragment exists. This class automatically initializes the maps system and the view; however you cannot be guaranteed when it will be ready because this depends on the availability of the Google Play services APK. If a GoogleMap is not available, getMap() will return null.

然后,在您的 Activity 中您可以使用 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 .

关于Android Google Map api v2 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16920293/

相关文章:

java - 在MapFragment下添加普通布局

android - 如何在 Google Map Api V2 Android 中搜索?

android - 使用 RecyclerView 在 Android 中拖放

android - 在电脑上通过 USB 远程控制 Android 设备,设备屏幕和声音必须传输到电脑

java - 完善的工程算法

android - 我的 Android 虚拟设备出了什么问题?

android - 如何在不锁定 UI 线程的情况下快速向 Google Map API v2 中的 MapFragment 添加多个标记?

javascript - 谷歌地图 API : Marker image positioning

php - 我可以通过 POST/GET 发送多少字节?

android - 混合和卫星 View 在 android google maps api 中不起作用