我正在尝试将地图添加到片段中,但我的IDE中没有出现错误的迹象,但该应用只是在设备中崩溃了。我不知道发生了什么我已经在控制台中启用了适用于Android的地图SDK,而我所做的只是将Android Studio生成的MapsActivity修改为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>
我只有这个错误。
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@13280056@13.2.80 (040800-211705629):3)
at eo.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):4)
at eq.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):55)
at com.google.maps.api.android.lib6.drd.ap.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):11)
at dx.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):17)
at dx.run(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (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@13280056@13.2.80 (040800-211705629):25)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at ep.b(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):3)
at eo.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):4)
at eq.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):55)
at com.google.maps.api.android.lib6.drd.ap.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):11)
at dx.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):17)
at dx.run(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):65)
答案 0 :(得分:5)
如果目标api级别为28或更高,则必须在清单中包括:
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
答案 1 :(得分:1)
在Android 6.0中,Google已删除了对Apache HTTP客户端的支持,并且从Android 9开始,该库已从bootclasspath中删除,并且默认情况下不适用于应用程序。
面向Android 9及更高版本的应用可以将以下内容添加到其AndroidManifest.xml中:
在build.gradle中,您需要添加它以支持android 6.0以上的apache
useLibrary 'org.apache.http.legacy'
在Android Manifest文件中声明要支持9.0以上版本
<uses-library android:name="org.apache.http.legacy" android:required="false"/>