pro*_*roG 9 java android google-maps
我无法得到地图!我能得到的只是空的.
这是代码.
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);
} }
}
Run Code Online (Sandbox Code Playgroud)
清单:
<?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>
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
我尝试解决它就像这个解决方案谷歌地图Android API v2抛出GooglePlayServicesNotAvailableException,过时,SupportMapFragment.getMap()返回null ..但仍然无法正常工作
更新:
我现在正在使用我的地图..这是编辑的工作版本
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);
}
Run Code Online (Sandbox Code Playgroud)
Com*_*are 17
您正在创建一个动态片段FragmentTransaction
.当你打电话时commit()
,片段还没有被添加到屏幕上,因为FragmentTransaction
它只被安排发生 - 它还没有发生.因此,SupportMapFragment
尚未调用onCreateView()
,所以没有GoogleMap
.
切换到静态片段(<fragment>
布局中的标记),或延迟使用GoogleMap
处理事务后的直到.
FragmentManager类中的executePendingTransactions()旨在修复此延迟.来自documantstion:在使用FragmentTransaction.commit()提交FragmentTransaction之后,它被安排在进程的主线程上异步执行.如果要立即执行任何此类挂起操作,可以调用此函数(仅从主线程)来执行此操作.请注意,所有回调和其他相关行为都将在此调用中完成,因此请注意调用它的位置.
我扩展了MapFragment类并添加了一个监听器.关于getMap()的文档说:
...如果片段的视图尚未准备好,则为空.如果片段生命周期尚未通过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;
}
Run Code Online (Sandbox Code Playgroud)
}
在调用活动或片段之后......
mapFragment = MySupportMapFragment.newInstance();
mapFragment.setListener(new MySupportMapFragmentListener() {
@Override
public void onMapCreated(GoogleMap googleMap) {
// TODO Auto-generated method stub
if(googleMap!=null){
// service is unaviable
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18109 次 |
最近记录: |