B B B B - 1 month ago 6
Java Question

Get null for google maps while initializing Aeris Map View

I am following the tutorial to create a simple aeris map.

Error Details:

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);

And inside init method of AerisMapView.java(third party, aeris weather)

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:
}
}


My code:

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(project)

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'}

B B B B
Answer

The reason this was happening was that that my Google Play Services on my phone is outdated. The newest version of Android Studio is using latest API SDK to compile the app and hence google maps was returning null.