Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapFragment crash in Fragment in API 28

Tags:

android

kotlin

I'm trying to add a map into the fragment and I've got no sign of error in my IDE but the app just crashed in the device. I don't know what happened. I've enable my map SDK for Android in the console already and all I did was just modify the MapsActivity which generated by the Android Studio to MapsFragment.

MainActivity.kt

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

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

        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.add(R.id.fragment_container, MapsFragment.newInstance()).commit()
    }
}

MapsFragment.kt

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.gms.maps.*

import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions

class MapsFragment : Fragment(), OnMapReadyCallback {

    private lateinit var mMap: GoogleMap

    companion object {
        fun newInstance() = MapsFragment()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_maps, container, false)

        val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap

        // Add a marker in Sydney and move the camera
        val sydney = LatLng(3.0414067, 101.5901829)
        mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }
}

fragment_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsFragment">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

And all I got is this error.

E/AndroidRuntime: FATAL EXCEPTION: Thread-7
    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
        at ep.b(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):3)
        at eo.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):4)
        at eq.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):55)
        at com.google.maps.api.android.lib6.drd.ap.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):11)
        at dx.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):17)
        at dx.run(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):65)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/system/priv-app/PrebuiltGmsCore/app_chimera/m/MapsDynamite.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/00000006/n/x86_64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at ad.loadClass(:com.google.android.gms.dynamite_dynamiteloader@[email protected] (040800-211705629):25)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at ep.b(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):3) 
        at eo.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):4) 
        at eq.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):55) 
        at com.google.maps.api.android.lib6.drd.ap.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):11) 
        at dx.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):17) 
        at dx.run(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040800-211705629):65) 
like image 510
Froyo Avatar asked Nov 02 '18 09:11

Froyo


People also ask

What is MapFragment?

public class MapFragment extends Fragment. A Map component in an app. This fragment is the simplest way to place a map in an application. It's a wrapper around a view of a map to automatically handle the necessary life cycle needs.


Video Answer


2 Answers

If you target api level 28 or above, you must include in your Manifest:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

For your references: https://issuetracker.google.com/issues/79478779

like image 153
Cao Minh Vu Avatar answered Sep 21 '22 01:09

Cao Minh Vu


In Android 6.0,Google had removed support for the Apache HTTP client and beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default.

Apps that target Android 9 and above can add the following to their AndroidManifest.xml:

In the build.gradle you need to add this to support apache above android 6.0

 useLibrary 'org.apache.http.legacy'

Declare in the Android Manifest files to support above 9.0

 <uses-library android:name="org.apache.http.legacy" android:required="false"/>
like image 26
Ramesh Yankati Avatar answered Sep 21 '22 01:09

Ramesh Yankati