初始化Aeris Map View时,为谷地图获取null

时间:2015-12-07 11:27:35

标签: java android google-maps weather-api

我正在按照教程创建一个简单的航空地图。

错误详情:

12-04 11:55:51.216 22405-22405/usc.forecast.weather E/AndroidRuntime: FATAL EXCEPTION: main
  Process: usc.forecast.weather, PID: 22405
  java.lang.RuntimeException: Unable to start activity ComponentInfo{usc.forecast.weather/usc.forecast.weather.MapActivity}: java.lang.NullPointerException
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
   at android.app.ActivityThread.access$800(ActivityThread.java:135)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5001)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
   at com.hamweather.aeris.maps.AerisMapView.init(AerisMapView.java:219)
   at usc.forecast.weather.MapFragment.onCreateView(MapFragment.java:32)**
   at android.app.Fragment.performCreateView(Fragment.java:1700)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
   at android.app.BackStackRecord.run(BackStackRecord.java:684)
   at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
   at android.app.Activity.performStart(Activity.java:5240)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
   at android.app.ActivityThread.access$800(ActivityThread.java:135) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:136) 
   at android.app.ActivityThread.main(ActivityThread.java:5001) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:515)

mapView.init(savedInstanceState,AerisMapType.GOOGLE);

在AerisMapView.java(第三方,aeris weather

的init方法中
public void init(Bundle savedInstanceState, AerisMapView.AerisMapType type) {
        this.type = type;
        switch(null.$SwitchMap$com$hamweather$aeris$maps$AerisMapView$AerisMapType[type.ordinal()]) {
        case 1:
            this.mapView.onCreate(savedInstanceState);
            MapsInitializer.initialize(this.getContext());
            this.googleMap = this.mapView.getMap(); <-------
            this.googleMap.getUiSettings().setZoomControlsEnabled(false);
            this.googleMap.setOnCameraChangeListener(this);
            this.googleMap.setOnMarkerClickListener(this);
            this.markerAdapter = new AerisGoogleInfoAdapter();
            this.googleMap.setOnInfoWindowClickListener(this);
            this.googleMap.setInfoWindowAdapter(this.markerAdapter);
        default:
        }
    }

我的代码:

MapFragment.java

public class MapFragment extends MapViewFragment implements  OnAerisMapLongClickListener, AerisCallback {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        AerisEngine.initWithKeys(this.getString(R.string.aeris_client_id), this.getString(R.string.aeris_client_secret), "usc.forecast.weather");

        View view = inflater.inflate(R.layout.fragment_interactive_maps, container, false);
        mapView = (AerisMapView) view.findViewById(R.id.aerisfragment_map);
        mapView.init(savedInstanceState, AerisMapType.GOOGLE);

        Bundle bundle = getArguments();
        String lat = bundle.getString("lat");
        String lng = bundle.getString("lng");

        Location location = new Location("");
        location.setLatitude(Double.valueOf(lat));
        location.setLongitude(Double.valueOf(lng));

        mapView.moveToLocation(location);
        mapView.addLayer(AerisTile.RADSAT);

        mapView.setOnAerisMapLongClickListener(this);

        return view;
    }

    @Override
    public void onMapLongClick(double lat, double longitude) {
        // code to handle map long press. i.e. Fetch current conditions?
        // see demo app MapFragment.java
    }

    @Override
    public void onResult(EndpointType type, AerisResponse response) {

    }}

MapActivity.java

public class MapActivity extends ActionBarActivity {

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

    Intent intent = getIntent();
    String lat = intent.getStringExtra("lat");
    String lng = intent.getStringExtra("lng");


    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    MapFragment fragment = new MapFragment();

    Bundle bundle = new Bundle();
    bundle.putString("lat", lat);
    bundle.putString("lng", lng);

    fragment.setArguments(bundle);

    fragmentTransaction.add(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
}}

activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

fragment_interactive_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.hamweather.aeris.maps.AerisMapView
        android:id="@+id/aerisfragment_map"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </com.hamweather.aeris.maps.AerisMapView>
</LinearLayout>

的build.gradle(项目)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "usc.forecast.weather"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services-maps:8.3.0'
    compile 'com.facebook.android:facebook-android-sdk:4.6.0'
    compile 'com.hamweather:aeris-maps-library:1.1.1@aar'}

3 个答案:

答案 0 :(得分:1)

发生这种情况的原因是我手机上的Google Play服务已过时。最新版本的Android Studio使用最新的API SDK来编译应用程序,因此谷歌地图返回null。

答案 1 :(得分:0)

确保在这些生命周期事件中包含这些MapView次调用。

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

编辑:要使MapFragment初始化并开始使用它,通常需要这样做:

mapFragment.getMapAsync(this);

然后当它加载时,这个覆盖被调用然后你可以使用它(这可能就是为什么它太空了,因为你试图过早地使用它。)

@Override
public void onMapReady(GoogleMap googleMap) {

}

另外,请确保在FragmentActivity

中加入此工具声明
implements OnMapReadyCallback

MapFragment不需要像MapView那样绑定到生命周期中(上图),因为它是Fragment并且自己处理它。

答案 2 :(得分:0)

我看到这个问题链接到(当前)com.google.android.gms:play-services-maps:9.4版本的地图SDK,我相信这个问题也出现在9.2.1中。

Aeris已经认识到了这个问题,并建议您坚持使用com.google.android.gms:play-services:8.4.0直到它们出来并进行SDK更新(计划于2016年第四季度开始)。