Android 从布局创建的onCreate()中的getMap()提供nullpointerexception

Android 从布局创建的onCreate()中的getMap()提供nullpointerexception,android,nullpointerexception,supportmapfragment,Android,Nullpointerexception,Supportmapfragment,这是我的代码: import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; 在onCr

这是我的代码:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
在onCreate()方法内部:(我在onCreate()中只有这个,除了创建捆绑包和使用该捆绑包的变量指定之外。)

布局:

<fragment
    android:id="@+id/mapfragment"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

我刚刚注意到一件奇怪的事情,在stacktrace的第一行中,我的应用程序的完整软件包增加了一倍……为什么会发生这种情况?

您的地图还不可用。。。在使用播放服务之前,您可以使用以下方法来确定播放服务是否可用(我将在onActivityCreated中调用它,而不是onCreate中调用它):

此外,您的片段需要实现播放服务(接口GooglePlayServicesClient.ConnectionCallbacks和GooglePlayServicesClient.OnConnectionFailedListener:

    /*
     * Called by Location Services when the request to connect the
     * client finishes successfully. At this point, you can
     * request the current location or start periodic updates
     */
    @Override
    public void onConnected(Bundle dataBundle) {
        // Display the connection status
        Toast.makeText(context, "Connected to location services", Toast.LENGTH_SHORT).show();

    }

    /*
     * Called by Location Services if the connection to the
     * location client drops because of an error.
     */
    @Override
    public void onDisconnected() {
        // Display the connection status
        Toast.makeText(context, "Disconnected from location services. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

    /*
     * Called by Location Services if the attempt to
     * Location Services fails.
     */
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        /*
         * Google Play services can resolve some errors it detects.
         * If the error has a resolution, try sending an Intent to
         * start a Google Play services activity that can resolve
         * error.
         */
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(
                        SomeActivity,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
                /*
                 * Thrown if Google Play services canceled the original
                 * PendingIntent
                 */
            } catch (IntentSender.SendIntentException e) {
                // Log the error
                e.printStackTrace();
            }
        } else {
            /*
             * If no resolution is available, display a dialog to the
             * user with the error.
             */
            Toast.makeText(context, "Connection error", Toast.LENGTH_SHORT).show();
        }
    }
}
编辑: 以下是我在包含地图片段的活动中使用它的方式:

if (isPlayServicesAvailable()) {
    setContentView(R.layout.activity_with_map_fragment);
}
在片段中:

if (getMap() != null) {
    GoogleMap map = getMap();
    // do things with the map
}

这应该可以让您继续;-)

请发布日志,包括完整的异常堆栈跟踪。发布完整的onCreate函数代码可能是因为映射对象尚未就绪。看看演示API,它们使用回调来告诉它何时准备就绪。代码中的
map
是什么?你不能发布完整的代码吗?不…对不起,我不能。map是一个GoogleMap实例GoogleMap;一个全局变量。我要试试这个。嗯……我认为它没问题,因为isPlayServicesAvailable()返回false,因为我还没有安装最新的google play服务。我今天回来做这件事,现在它是最新的…我仍然得到了一个空指针异常…你的片段是否扩展了MapFragment()?结果是缺少权限。再次感谢!很高兴您找到了问题的根源;)
protected boolean isPlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (status == ConnectionResult.SUCCESS) {
        return (true);
    } else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
        // deal with error
    } else {
            // maps is not available
    }

    return (false);
}
    /*
     * Called by Location Services when the request to connect the
     * client finishes successfully. At this point, you can
     * request the current location or start periodic updates
     */
    @Override
    public void onConnected(Bundle dataBundle) {
        // Display the connection status
        Toast.makeText(context, "Connected to location services", Toast.LENGTH_SHORT).show();

    }

    /*
     * Called by Location Services if the connection to the
     * location client drops because of an error.
     */
    @Override
    public void onDisconnected() {
        // Display the connection status
        Toast.makeText(context, "Disconnected from location services. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

    /*
     * Called by Location Services if the attempt to
     * Location Services fails.
     */
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        /*
         * Google Play services can resolve some errors it detects.
         * If the error has a resolution, try sending an Intent to
         * start a Google Play services activity that can resolve
         * error.
         */
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(
                        SomeActivity,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
                /*
                 * Thrown if Google Play services canceled the original
                 * PendingIntent
                 */
            } catch (IntentSender.SendIntentException e) {
                // Log the error
                e.printStackTrace();
            }
        } else {
            /*
             * If no resolution is available, display a dialog to the
             * user with the error.
             */
            Toast.makeText(context, "Connection error", Toast.LENGTH_SHORT).show();
        }
    }
}
if (isPlayServicesAvailable()) {
    setContentView(R.layout.activity_with_map_fragment);
}
if (getMap() != null) {
    GoogleMap map = getMap();
    // do things with the map
}