Java 在谷歌地图V2。。。getMap()返回null
我拿不到地图!我只能得到空值 这是代码Java 在谷歌地图V2。。。getMap()返回null,java,android,google-maps,Java,Android,Google Maps,我拿不到地图!我只能得到空值 这是代码 public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SupportMapFragment fragment = new SupportMa
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);
} }
}
舱单:
<?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>
您正在通过
FragmentTransaction
创建动态片段。调用commit()
时,片段尚未添加到屏幕中,因为FragmentTransaction
仅计划发生,尚未发生。因此,SupportMapFragment
尚未使用onCreateView()
调用,因此没有GoogleMap
要么切换到静态片段(
布局中的标记),要么延迟使用谷歌地图
,直到事务处理完成。FragmentManager类中的executePendingTransactions()旨在修复此延迟。
From DocumantAction:使用FragmentTransaction.commit()提交FragmentTransaction后,计划在进程的主线程上异步执行该事务如果要立即执行任何此类挂起的操作,可以调用此函数(仅从主线程)来执行。请注意,所有回调和其他相关行为都将在此调用中完成,因此请注意调用的位置。另一种解决方法。在MapFragment中实现一个接口,以便在准备好创建GoogleMap时与活动通信
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
listener.onGoogleMapCreattion();
}
然后,您只需要在activtiy中实现侦听器:
@Override
public void onGoogleMapCreation() {
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = mMapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
setUpMap();
}
}
}
移动代码段:
谷歌地图;
map=fragment.getMap()
//这里的映射不是空的
if(map!=null){
}
对于onResume()方法,这将解决您的问题。我扩展了MapFragment类并添加了一个侦听器。 关于getMap()的文档说: 。。。如果片段的视图尚未就绪,则为Null。如果片段生命周期还没有经过onCreateView(LayoutInflater、ViewGroup、Bundle)的检查,就会发生这种情况 然后在onCreateView之后调用侦听器。我添加了这个类
public class MySupportMapFragment extends SupportMapFragment{
private MySupportMapFragmentListener listener;
public interface MySupportMapFragmentListener{
public void onMapCreated(GoogleMap googleMap);
}
// value taken from source code
private static final String MAP_OPTIONS = "MapOptions";
public static MySupportMapFragment newInstance() {
MySupportMapFragment f = new MySupportMapFragment();
return f;
}
public static MySupportMapFragment newInstance(GoogleMapOptions options) {
MySupportMapFragment f = new MySupportMapFragment();
Bundle args = new Bundle();
args.putParcelable(MAP_OPTIONS, options);
f.setArguments(args);
return f;
}
// other newInstance methods ...
/* (non-Javadoc)
* @see android.support.v4.app.Fragment#onViewCreated(android.view.View, android.os.Bundle)
*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
// here, as doc say, the map can be initialized, or the service is not available
if(listener!=null){
listener.onMapCreated(getMap());
}
}
/**
* @return the listener
*/
public MySupportMapFragmentListener getListener() {
return listener;
}
/**
* @param listener the listener to set
*/
public void setListener(MySupportMapFragmentListener listener) {
this.listener = listener;
}
}
在调用活动或片段之后
mapFragment = MySupportMapFragment.newInstance();
mapFragment.setListener(new MySupportMapFragmentListener() {
@Override
public void onMapCreated(GoogleMap googleMap) {
// TODO Auto-generated method stub
if(googleMap!=null){
// service is unaviable
}
}
抱歉:$但是我怎么能延迟使用GoogleMap呢?我试着在onResume()中调用它,但没有成功。。我不确定这是否是你想要的mean@proG:在事务上调用
commit()
后,在某些视图上调用post()。当调用您计划的Runnable
时,GoogleMap
应该存在。我通过xml布局使用静态片段,但有些手机仍然存在这个问题。一个测试仪的GS2工作,而另一个测试仪的GS2不工作。这可能与设备上没有Google Play服务有关吗?@jmrbooties:不,在尝试使用Maps V2进行任何操作之前,您早就应该知道这是问题所在。在调用fragMan.executePendingTransactions()
之后,我也无法获取地图,发生相同的NullPointerException我也尝试过执行executePendingTransactions
,调用后仍然为null。您的解决方案更新使它对我有效+1最佳答案。处理创建过程的异步性质是正确的方法。还有其他方法吗?您只是猜测现在是访问对象的正确时间。非常好的解决方案,目前似乎是最佳解决方案。您不能直接使用onViewCreated吗?
UiSettings mm = map.getUiSettings();
map.setMyLocationEnabled(true);
public class MySupportMapFragment extends SupportMapFragment{
private MySupportMapFragmentListener listener;
public interface MySupportMapFragmentListener{
public void onMapCreated(GoogleMap googleMap);
}
// value taken from source code
private static final String MAP_OPTIONS = "MapOptions";
public static MySupportMapFragment newInstance() {
MySupportMapFragment f = new MySupportMapFragment();
return f;
}
public static MySupportMapFragment newInstance(GoogleMapOptions options) {
MySupportMapFragment f = new MySupportMapFragment();
Bundle args = new Bundle();
args.putParcelable(MAP_OPTIONS, options);
f.setArguments(args);
return f;
}
// other newInstance methods ...
/* (non-Javadoc)
* @see android.support.v4.app.Fragment#onViewCreated(android.view.View, android.os.Bundle)
*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
// here, as doc say, the map can be initialized, or the service is not available
if(listener!=null){
listener.onMapCreated(getMap());
}
}
/**
* @return the listener
*/
public MySupportMapFragmentListener getListener() {
return listener;
}
/**
* @param listener the listener to set
*/
public void setListener(MySupportMapFragmentListener listener) {
this.listener = listener;
}
mapFragment = MySupportMapFragment.newInstance();
mapFragment.setListener(new MySupportMapFragmentListener() {
@Override
public void onMapCreated(GoogleMap googleMap) {
// TODO Auto-generated method stub
if(googleMap!=null){
// service is unaviable
}
}