Search code examples
androidandroid-fragmentskotlinmapboxmapbox-android

Instantiating MapBox before inflating the fragment layout cause Exception


I'm following MabBox tutorial at https://docs.mapbox.com/help/tutorials/android-navigation-sdk/ to make a simple navigation fragment that use the basics elements of it.

I already know that MapBox require to instantiate before inflating the View because I made it before on another Fragment to test it out where the fragment is part of a TabLayout component, however, in this one, there's only one Activity that loads the Fragment where I use the SDK and in this case it throws the same error that I fixed previously because I put it afterinflating the layout.

The errore that appears is this one:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.parkingclientapplication, PID: 14257
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.parkingclientapplication/com.example.parkingclientapplication.activities.MapDirectionActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class com.mapbox.mapboxsdk.maps.MapView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class com.mapbox.mapboxsdk.maps.MapView
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.mapbox.mapboxsdk.maps.MapView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.example.parkingclientapplication.activities.MapDirectionActivity.onCreate(MapDirectionActivity.kt:15)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException: 
    Using MapView requires calling Mapbox.getInstance(Context context, String accessToken) before inflating or creating the view. The access token parameter is required when using a Mapbox service.
    Please see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one.
    More information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens.
        at com.mapbox.mapboxsdk.Mapbox.validateMapbox(Mapbox.java:165)
        at com.mapbox.mapboxsdk.Mapbox.getAccessToken(Mapbox.java:77)
        at com.mapbox.mapboxsdk.storage.FileSource.<init>(FileSource.java:234)
        at com.mapbox.mapboxsdk.storage.FileSource.getInstance(FileSource.java:68)
        at com.mapbox.mapboxsdk.maps.renderer.MapRenderer.<init>(MapRenderer.java:34)
E/AndroidRuntime:     at com.mapbox.mapboxsdk.maps.renderer.glsurfaceview.GLSurfaceViewMapRenderer.<init>(GLSurfaceViewMapRenderer.java:33)
        at com.mapbox.mapboxsdk.maps.MapView$5.<init>(MapView.java:294)
        at com.mapbox.mapboxsdk.maps.MapView.initialiseDrawingSurface(MapView.java:294)
        at com.mapbox.mapboxsdk.maps.MapView.initialize(MapView.java:137)
        at com.mapbox.mapboxsdk.maps.MapView.<init>(MapView.java:100)
            ... 25 more

The code for the Activity is:

class MapDirectionActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_map_direction)

        supportFragmentManager.inTransaction {
            add(R.id.form_clientNavMenu, MapDirectionFragment())
        }
        //Logout action on custom toolbar layout
        logout.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
    }
}

The code for the onCreateView method of the Fragment is:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        Mapbox.getInstance(context!!, getString(R.string.access_token))
        val view = inflater.inflate(R.layout.fragment_map_direction, container, false)

        mapView = view.findViewById(R.id.mapView)
        mapView.onCreate(savedInstanceState)
        mapView.getMapAsync(this)
        // Inflate the layout for this fragment
        return view
}

I only wrote onCreateView method because I think the error could be in there instead on the rest of the code which is the same as in the link, but I could be wrong.


Solution

  • Thanks @Tobrun for the answer, the problem was not that I missed the accessToken but I mixed activity and fragment layout so the components in one of them should be in the other. However, looking for the accessToken made me realize about other possible ones.