Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android get映射上的空指针异常_Android_Google Maps - Fatal编程技术网

Android get映射上的空指针异常

Android get映射上的空指针异常,android,google-maps,Android,Google Maps,我正在实现GoogleMapv2API,并获取当前位置。我使用了与Google示例项目完全相同的代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_map); } @Override protected void onResume() { supe

我正在实现GoogleMapv2API,并获取当前位置。我使用了与Google示例项目完全相同的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_map);
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
    setUpLocationClientIfNeeded();
    mLocationClient.connect();
}

@Override
public void onPause() {
    super.onPause();
    if (mLocationClient != null) {
        mLocationClient.disconnect();
    }
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_AddFragment))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            mMap.setMyLocationEnabled(true);
            mMap.setOnMyLocationButtonClickListener(this);
        }
    }
}
我的布局:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"        
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment 
    android:id="@+id/map_AddFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment"
    tools:ignore="MissingPrefix" />
</RelativeLayout>

当我从示例项目运行此代码时,它可以工作,但当我将此代码复制到我的项目时,它会在
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map_AddHTag))行上给我一个
Null指针异常
.getMap()

我真的不知道发生了什么,因为它运行良好,在示例项目中仍然运行良好


谢谢。

好吧,你应该被一个
ClassCastException
搞砸了。您的布局具有
com.google.android.gms.maps.MapFragment
,您试图将其转换为
SupportMapFragment
,但这将不起作用。如果这是一个
FragmentActivity
,您需要在布局中以及Java代码中使用
SupportMapFragment

如何将
SupportMapFragment
添加到活动中?请尝试Project->Clean。如果您在XML文件中移动了内容,这通常可以解决问题。@commonware谢谢,我已经更新了我的帖子。我还清理了这个项目,关闭了eclipse并重新打开了它,但仍然没有工作哇!你完全正确!我甚至没有注意到。。。多谢各位!