在谷歌地图V2 ... fragment.getMap()返回null [英] in google maps V2 ... fragment.getMap() returns null
问题描述
我不能得到的地图!所有我能得到的是空。
这里是code。
公共类MainActivity扩展FragmentActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
SupportMapFragment片段=新SupportMapFragment();
getSupportFragmentManager()的BeginTransaction()
。新增(android.R.id.content,片段).commit();
GoogleMap的地图;
地图= fragment.getMap();
//这里我不能访问这个片段,因为地图= NULL
如果(图!= NULL){
UiSettings毫米= map.getUiSettings();
map.setMyLocationEnabled(真正的);
}}
}
该清单:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.anywhere_reminder
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =17/>
<用途特征
机器人:glEsVersion =0x00020000
机器人:要求=真/>
<许可
机器人:名称=com.example.anywhere_reminder.permission.MAPS_RECEIVE
安卓的ProtectionLevel =签名/>
<使用-权限的Android:名称=com.example.anywhere_reminder.permission.MAPS_RECEIVE/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<使用-权限的Android:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Android:名称=android.permission.ACCESS_FINE_LOCATION/>
<使用-权限的Android:名称=com.google.android.providers.gsf.permission.READ_GSERVICES/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动机器人:名称=com.google.android.gms.BuildConfig/>
<活动
机器人:名称=com.example.anywhere_reminder.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
&所述;元数据
机器人:名称=com.google.android.maps.v2.API_KEY
机器人:值=我的钥匙/>
< /用途>
< /舱单>
下面是XML:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>
<片段的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /图
机器人:layout_width =match_parent
机器人:layout_height =match_parent
类=com.google.android.gms.maps.MapFragment/>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:文本=@字符串/参考hello world/>
< / RelativeLayout的>
我试图解决这个问题是这样解决的<一个href="http://stackoverflow.com/questions/13722192/google-maps-android-api-v2-throws-googleplayservicesnotavailableexception-out-o">Google地图Android的API V2抛出GooglePlayServicesNotAvailableException的,过时的,SupportMapFragment.getMap()返回null ..但仍然没有工作
更新:
我得到了我的地图,现在工作..这里是编辑的工作版本
公共类MainActivity扩展FragmentActivity {
私人GoogleMap的MYMAP;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment
=(SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
MYMAP = mySupportMapFragment.getMap();
如果(MYMAP!= NULL)
myMap.setMyLocationEnabled(真正的);
}
您正在创建一个动态的片段,通过 FragmentTransaction
。当你调用提交()
,该片段具有的没有的还没有被添加到屏幕上,因为 FragmentTransaction
只被预定要发生 - 这尚未发生。因此, SupportMapFragment
尚未调用 onCreateView()
然而,所以没有 GoogleMap的
。
更换为静态的片段(&LT;片断&gt;
标签的布局),或耽误您的使用 GoogleMap的
直到交易后已被处理。
I can't get the map ! all I can get is null.
here is the code.
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
GoogleMap map;
map = fragment.getMap();
//here i cant access this snippet because map = null
if(map!=null){
UiSettings mm = map.getUiSettings();
map.setMyLocationEnabled(true);
} }
}
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.anywhere_reminder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.example.anywhere_reminder.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.anywhere_reminder.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.google.android.gms.BuildConfig" />
<activity
android:name="com.example.anywhere_reminder.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value=" my key "/>
</application>
</manifest>
here is the xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
I tried to solve it like this solution Google Maps Android API v2 throws GooglePlayServicesNotAvailableException, out of date, SupportMapFragment.getMap() returns null .. but still did not work
UPDATE:
I got my map working now .. here is the edited working version
public class MainActivity extends FragmentActivity {
private GoogleMap myMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment
= (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = mySupportMapFragment.getMap();
if(myMap!=null)
myMap.setMyLocationEnabled(true);
}
You are creating a dynamic fragment, via a FragmentTransaction
. When you call commit()
, the fragment has not yet been added to the screen, because the FragmentTransaction
has only been scheduled to occur -- it has not occurred yet. Hence, the SupportMapFragment
has not been called with onCreateView()
yet, so there is no GoogleMap
.
Either switch to static fragments (<fragment>
tag in a layout), or delay your use of the GoogleMap
until after the transaction has been processed.
这篇关于在谷歌地图V2 ... fragment.getMap()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!