![](/img/trans.png)
[英]Error inflating class com.google.android.gms.maps.MapView
[英]android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.gms.maps.MapView
我試圖用viewpager在片段中實現mapview,但是盡管我嘗試了很多解決方案,但我找不到適合我項目的正確解決方案。
這是我的logcat
2019-05-26 17:28:16.645 12469-12469/com.example.emrullah.booksharingappv4 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.emrullah.booksharingappv4, PID: 12469
android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class com.google.android.gms.maps.MapView
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.gms.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:651)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at com.example.emrullah.booksharingappv4.Fragments.MapviewFragment.onCreateView(MapviewFragment.java:50)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)
這是我的片段
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mapview_fragment, container, false);
mLocationList= new ArrayList<>();
mLocList= new ArrayList<>();
mLocList2= new ArrayList<>();
mRef=FirebaseDatabase.getInstance().getReference().child("users");
currentUser=FirebaseAuth.getInstance().getCurrentUser();
getLatLongFromDb();
split(mLocList);
mMapView = view.findViewById(R.id.map_view);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
//Gets the Googlemap from Mapview and does the initialization stuff
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
for (int i = 0;i<mLocList2.size();i++){
LatLng sydney = new LatLng(mLocList2.get(i),mLocList2.get(i+1));
mMap.addMarker(new MarkerOptions().position(sydney).title("Book dest:"+(i+1)));
}
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
if (mMapView != null)
mMapView.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mMapView != null)
mMapView.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mMapView != null)
mMapView.onSaveInstanceState(outState);
}
}
這是我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.emrullah.booksharingappv4.Fragments.MapviewFragment"
/>
</RelativeLayout>
並且Activity中的viewPager部分
public void setupViewPager(ViewPager viewPager) {
SectionPageAdapter sectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
sectionPageAdapter.addFragment(new BookListFragment(), "BOOKS");
sectionPageAdapter.addFragment(new MapviewFragment(), "BOOK MAP");
sectionPageAdapter.addFragment(new SettingsFragment(), "SETTINGS");
viewPager.setAdapter(sectionPageAdapter);
}
public void setUpTabIcons(){
tabLayout.getTabAt(0).setIcon(navIcons[0]);
tabLayout.getTabAt(1).setIcon(navIcons[1]);
tabLayout.getTabAt(2).setIcon(navIcons[2]);
}
我知道有一些像我這樣的類似問題,但我嘗試了所有這些問題,但沒有一個能夠奏效。 謝謝您的幫助。
您正在嘗試使用Android版Google地圖支持的原始版本(com.google.android.maps)。 我們現在稱之為地圖V1。
這不起作用,因為地圖V1已被棄用超過4年。 你不能再獲得它的API密鑰,所以即使你的代碼是正確的,它也行不通。 崩潰是因為舊的MapView必須在MapActivity中使用,如錯誤所示。
當前版本的Google地圖支持Android - Maps V2 - 具有非常不同的API,並且不涉及com.google.android.maps包中的類。
請閱讀如何使用Maps V2形式的官方文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.