无法获取GoogleMap对象

时间:2014-02-04 04:46:37

标签: java android google-maps

奇怪的是map = frag.getMap();不会抛出nullpointerexception,但地图仍然是null!

运行或不运行处理程序的相同情况。

我看到地图(世界地图),但无法对地图对象做任何事情。

包含项目属性的PRINT SCREE:http://i.stack.imgur.com/BGB65.jpg

请查看代码,请在我错的地方提出建议。

package com.bestsiteeditor.bestwirelessdeals;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;


import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;




 public class Settings extends FragmentActivity{
    LinearLayout submenu;

     GoogleMap map;
    LinearLayout mainLayout;
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

         submenu =(LinearLayout) findViewById(R.id.submenu);
         SubMenu sm = new SubMenu(this,submenu);
         sm.getButtons();


       mainLayout  =(LinearLayout) findViewById(R.id.temp);



      final Handler handler = new Handler();
      handler.postDelayed(new Runnable(){

         @Override
         public void run() {


       MapFragment frag = MapFragment.newInstance(); 
      FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
      fragmentTransaction.add(mainLayout.getId(), frag);

      fragmentTransaction.commit();  

     map=frag.getMap();
      System.out.println("THEMAAP "+map);

      if(map != null) {
          System.out.println("THEMAAP NOT NULL "+map);
      }else{handler.postDelayed(this, 500);}

         }
      }, 500);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {

        return ActionBar.HandleMenu(this, item.getItemId());

    }


}

并且是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bestsiteeditor.bestwirelessdeals"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="19" />


    <permission
        android:name="com.suntrainer.suntrainer.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.suntrainer.suntrainer.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" >
    </uses-permission>



    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyAppActionBarTheme" >


        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDJuwvAd8Zzqasdasdadasdasd" />

        <activity
            android:name="com.bestsiteeditor.bestwirelessdeals.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="sensorPortrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.bestsiteeditor.bestwirelessdeals.Locations"
            android:label="@string/app_name"
            android:screenOrientation="sensorPortrait" >
            <intent-filter>
                <action android:name="android.intent.action.LOCATIONS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.bestsiteeditor.bestwirelessdeals.Settings"
            android:label="@string/app_name"
            android:screenOrientation="sensorPortrait" >
            <intent-filter>
                <action android:name="android.intent.action.SETTINGS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.bestsiteeditor.bestwirelessdeals.WatchList"
            android:label="@string/app_name"
            android:screenOrientation="sensorPortrait" >
            <intent-filter>
                <action android:name="android.intent.action.WATCHLIST" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.bestsiteeditor.bestwirelessdeals.Profile"
            android:label="@string/app_name"
            android:screenOrientation="sensorPortrait" >
            <intent-filter>
                <action android:name="android.intent.action.PROFILE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

你有

android:minSdkVersion="12"

所以你需要改变

public class Settings extends FragmentActivity{

 public class Settings extends Activity{   

此外,最好在初始化之前检查Google Play服务的可用性 谷歌地图对象。

来自文档

  

公共最终GoogleMap getMap()

     

获取与此包装的视图关联的基础GoogleMap   片段。

     

返回GoogleMap。如果片段的视图尚未显示为空   准备。如果片段生命周期没有通过,就会发生这种情况   onCreateView(LayoutInflater,ViewGroup,Bundle)呢。这也可以   如果Google Play服务不可用,则会发生。如果谷歌播放   服务随后可用,片段已经消失   通过onCreateView(LayoutInflater,ViewGroup,Bundle),调用它   方法将再次初始化并返回GoogleMap。

编辑:

在manifest fie的applciation标记中缺少元标记。

请确保您遵循了所有步骤@

https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

答案 1 :(得分:0)

片段交易解决问题后,只需等待500毫秒。

也可以实现处理程序而不是Thread。

fragmentTransaction.commit();  

Thread thread=  new Thread(){
    @Override
    public void run(){ try { synchronized(this){wait(500);}}catch(InterruptedException ex){ }

        map=frag.getMap();
        System.out.println("MAP OK"+frag.getMap());

    }
};

thread.start();