diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp index 37ed35f77..497091832 100644 --- a/android/cpp/jni.cpp +++ b/android/cpp/jni.cpp @@ -43,6 +43,7 @@ jfieldID lon_lat_zoom_lat_id = nullptr; jfieldID lon_lat_zoom_zoom_id = nullptr; jclass runtime_exception_class = nullptr; +jclass null_pointer_exception_class = nullptr; jmethodID list_to_array_id = nullptr; @@ -61,16 +62,28 @@ bool throw_error(JNIEnv* env, const char* msg) { std::string std_string_from_jstring(JNIEnv* env, jstring jstr) { std::string str; + + if (jstr == nullptr) { + if (env->ThrowNew(null_pointer_exception_class, "String cannot be null.") < 0) { + env->ExceptionDescribe(); + return str; + } + + return str; + } + jsize len = env->GetStringLength(jstr); if (len < 0) { env->ExceptionDescribe(); return str; } + const jchar* chars = env->GetStringChars(jstr, nullptr); if (chars == nullptr) { env->ExceptionDescribe(); return str; } + std::u16string ustr(reinterpret_cast(chars), len); env->ReleaseStringChars(jstr, chars); chars = nullptr; @@ -93,6 +106,15 @@ jstring std_string_to_jstring(JNIEnv* env, std::string str) { std::vector std_vector_string_from_jobject(JNIEnv* env, jobject jlist) { std::vector vector; + if (jlist == nullptr) { + if (env->ThrowNew(null_pointer_exception_class, "List cannot be null.") < 0) { + env->ExceptionDescribe(); + return vector; + } + + return vector; + } + jobjectArray array = reinterpret_cast(env->CallObjectMethod(jlist, list_to_array_id)); if (env->ExceptionCheck() || (array == nullptr)) { env->ExceptionDescribe(); @@ -757,6 +779,12 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } + null_pointer_exception_class = env->FindClass("java/lang/NullPointerException"); + if (null_pointer_exception_class == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + jclass list_class = env->FindClass("java/util/List"); if (list_class == nullptr) { env->ExceptionDescribe(); @@ -877,12 +905,22 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } + null_pointer_exception_class = reinterpret_cast(env->NewGlobalRef(null_pointer_exception_class)); + if (runtime_exception_class == nullptr) { + env->ExceptionDescribe(); + env->DeleteGlobalRef(lon_lat_class); + env->DeleteGlobalRef(lon_lat_zoom_class); + env->DeleteGlobalRef(runtime_exception_class); + return JNI_ERR; + } + array_list_class = reinterpret_cast(env->NewGlobalRef(array_list_class)); if (array_list_class == nullptr) { env->ExceptionDescribe(); env->DeleteGlobalRef(lon_lat_class); env->DeleteGlobalRef(lon_lat_zoom_class); env->DeleteGlobalRef(runtime_exception_class); + env->DeleteGlobalRef(null_pointer_exception_class); return JNI_ERR; } @@ -917,6 +955,9 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) { env->DeleteGlobalRef(runtime_exception_class); runtime_exception_class = nullptr; + env->DeleteGlobalRef(null_pointer_exception_class); + null_pointer_exception_class = nullptr; + list_to_array_id = nullptr; env->DeleteGlobalRef(array_list_class); diff --git a/android/java/app/src/main/res/layout/activity_main.xml b/android/java/app/src/main/res/layout/activity_main.xml index e4b11e3ad..bdfaaec15 100644 --- a/android/java/app/src/main/res/layout/activity_main.xml +++ b/android/java/app/src/main/res/layout/activity_main.xml @@ -1,11 +1,13 @@ + xmlns:tools="http://schemas.android.com/tools" + xmlns:app="http://schemas.android.com/apk/res-auto" + tools:context="${packageName}.${activityClass}"> + android:id="@+id/map" + app:styleUrl="asset://styles/styles/bright-v6.json"/> diff --git a/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java b/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java index 530d563dc..d86a0df74 100644 --- a/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java +++ b/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java @@ -86,7 +86,7 @@ public class MapView extends SurfaceView { private boolean mZoomEnabled = true; private boolean mScrollEnabled = true; private boolean mRotateEnabled = true; - private String mStyleUrl = "asset://styles/styles/bright-v6.json"; + private String mStyleUrl; // // Constructors @@ -137,7 +137,6 @@ public class MapView extends SurfaceView { // Create the NativeMapView mNativeMapView = new NativeMapView(this, cachePath, dataPath, apkPath); - mNativeMapView.setStyleUrl(mStyleUrl); // Load the attributes TypedArray typedArray = context.obtainStyledAttributes(attrs, @@ -164,8 +163,6 @@ public class MapView extends SurfaceView { R.styleable.MapView_debugActive, false)); setStyleUrl(typedArray.getString( R.styleable.MapView_styleUrl)); - setAccessToken(typedArray.getString( - R.styleable.MapView_accessToken)); } finally { typedArray.recycle(); } diff --git a/android/java/lib/src/main/res/values/attrs.xml b/android/java/lib/src/main/res/values/attrs.xml index 191e7d000..770b70317 100644 --- a/android/java/lib/src/main/res/values/attrs.xml +++ b/android/java/lib/src/main/res/values/attrs.xml @@ -8,7 +8,6 @@ - @@ -20,6 +19,5 @@ - diff --git a/include/mbgl/android/jni.hpp b/include/mbgl/android/jni.hpp index 28b9b8917..2dbdf2ebe 100644 --- a/include/mbgl/android/jni.hpp +++ b/include/mbgl/android/jni.hpp @@ -25,6 +25,7 @@ extern jfieldID lon_lat_zoom_lat_id; extern jfieldID lon_lat_zoom_zoom_id; extern jclass runtime_exception_class; +extern jclass null_pointer_exception_class;; extern jmethodID list_to_array_id;