加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在谷歌地图V2 … fragment.getMap()返回null

发布时间:2020-12-14 16:33:24 所属栏目:Java 来源:网络整理
导读:我无法得到地图!我可以得到的都是null. 这里是代码. public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SupportMapFragment fragment = new Sup
我无法得到地图!我可以得到的都是null.

这里是代码.

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>

这里是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>

我试图解决它像这个解决方案Google Maps Android API v2 throws GooglePlayServicesNotAvailableException,out of date,SupportMapFragment.getMap() returns 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);


        }

解决方法

您正在通过FragmentTransaction创建动态片段.当调用commit()时,片段尚未添加到屏幕中,因为FragmentTransaction仅被安排发生 – 它还没有发生.因此,SupportMapFragment还没有被onCreateView()调用,所以没有GoogleMap.

切换到静态片段(布局中的< fragment>标签),或延迟使用GoogleMap,直到处理完成.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读