Android 当我使用FragmentManager查找地图时,为什么返回空值?
嗨,当我从片段管理器对象获取映射时,我去寻找映射片段,它返回null,为什么 我的代码是:Android 当我使用FragmentManager查找地图时,为什么返回空值?,android,google-maps,android-fragments,adt,gis,Android,Google Maps,Android Fragments,Adt,Gis,嗨,当我从片段管理器对象获取映射时,我去寻找映射片段,它返回null,为什么 我的代码是: public class MapaFragment extends Fragment { ... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = super.onCreate
public class MapaFragment extends Fragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
android.app.FragmentManager fragmentManager = getFragmentManager();
MapFragment mapFragment = (MapFragment) fragmentManager.findFragmentById(R.id.map);
GoogleMap map = mapFragment.getMap();
return v;
}
...
}
mapFragment中的代码为空,显然我无法获取GoogleMap。LogCat给我那行的空指针错误
我的片段是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
我认为权限和清单是正确的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<permission
android:name="info.android.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="info.android.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
<meta-data
android:name="com.google.android.gms.version"
android:value="6171000" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".LoginActivity"
android:label="Login" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ImagenActivity"
android:label="@string/title_activity_imagen" >
</activity>
</application>
</manifest>
我得到这个屏幕:
我使用的配置屏幕是:
编辑:您用于测试的设备需要Google Play服务。有关设置仿真器的指南,请参阅。 将布局的XML更改为,并注意XML文件的名称:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
</LinearLayout>
另一个选项:扩展MapFragment此选项与上一个选项完全无关。在这种情况下,您不会创建、分配或膨胀XML文件。视图是以编程方式生成的
public static class MapaFragment extends MapFragment {
GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2){
View v = super.onCreateView(arg0,arg1,arg2);
mMap = getMap();
return v;
}
}
编辑:
我认为您实际上并没有夸大布局,因为您调用的是超级方法
尝试改变
View v = super.onCreateView(inflater, container, savedInstanceState);
致:
注意:用片段布局名称替换
R.id.yourLayout
。@David噢,我的错。更新了我的答案。
View v = super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.id.yourLayout, container, false);