程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Google Maps V2中……fragment.getMap()返回null大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在Google Maps V2中……fragment.getMap()返回null?

开发过程中遇到在Google Maps V2中……fragment.getMap()返回null的问题如何解决?下面主要结合日常开发的经验,给出你关于在Google Maps V2中……fragment.getMap()返回null的解决方法建议,希望对你解决在Google Maps V2中……fragment.getMap()返回null有所启发或帮助;

您正在通过创建动态片段Fragmenttransaction。当你调用commit(),片段还 没有 还没有被添加到屏幕上,因为Fragmenttransaction只被预定要发生- 没有发生呢。因此,SupportMapFragment尚未调用onCreateVIEw(),因此没有GoogleMap

要么切换到静态片段(<fragment>布局中的标记),要么将您的使用延迟GoogleMap到事务处理完成之后。

解决方法

我找不到地图!我只能得到的是空值。

这是代码。

       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://scheR_741_11845@as.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>

这是xml:

  <RelativeLayout xmlns:android="http://scheR_741_11845@as.android.com/apk/res/android"
 xmlns:tools="http://scheR_741_11845@as.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity" >


 <fragment xmlns:android="http://scheR_741_11845@as.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>

我试图像这种解决方案一样解决它Google Maps 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);


        }

大佬总结

以上是大佬教程为你收集整理的在Google Maps V2中……fragment.getMap()返回null全部内容,希望文章能够帮你解决在Google Maps V2中……fragment.getMap()返回null所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:在Google