Mapbox在片段上膨胀视图

我有一个Android的片段上的Mapbox MapView的膨胀问题。 当我在Activity上使用相同的代码(除了Activity和Fragment之间的标准差异),我可以正确加载MapView,但不能在Fragment上加载。

Mapbox 在这里也有一个MapFragment类,但我不确定如何在Fragment中正确使用它(这个例子仍然在Activity中)。 与谷歌地图API我能够使用片段上的MapView没有问题。

下面的logcat行:

at com.example.exampleapp.FragmentMap.onCreateView(FragmentMap.java:133) 

是指Java行:

 fragmentLayout = inflater.inflate(R.layout.fragment_map, container, false); 

以下是与该日志一起的这个问题的相关代码:

fragment_map.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:mapbox="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.mapbox.mapboxsdk.views.MapView android:id="@+id/mapview" android:layout_width="match_parent" android:layout_height="match_parent" mapbox:access_token="@string/access_token"/> <android.support.v7.widget.CardView android:id="@+id/map_card" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginLeft="5dp" android:layout_marginTop="5dp"> <com.mapbox.mapboxsdk.views.MapView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/mini_map" android:layout_centerVertical="true" android:layout_centerHorizontal="true" mapbox:access_token="@string/access_token"/> </android.support.v7.widget.CardView> 

FragmentMap.java:

 public class FragmentMap extends Fragment { private MapView mv, miniMap; View fragmentLayout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { fragmentLayout = inflater.inflate(R.layout.fragment_map, container, false); mv = (MapView) fragmentLayout.findViewById(R.id.mapview); mv.onCreate(savedInstanceState); miniMap = (MapView) fragmentLayout.findViewById(R.id.mini_map); miniMap.onCreate(savedInstanceState); return fragmentLayout; } @Override public void onStart() { super.onStart(); mv.onStart(); miniMap.onStart(); } @Override public void onStop() { super.onStop(); mv.onStop(); miniMap.onStop(); } @Override public void onDestroy() { super.onDestroy(); mv.onDestroy(); miniMap.onDestroy(); } @Override public void onResume() { super.onResume(); mv.onResume(); miniMap.onResume(); } @Override public void onPause() { super.onPause(); mv.onPause(); miniMap.onPause(); } @Override public void onLowMemory() { super.onLowMemory(); mv.onLowMemory(); miniMap.onLowMemory(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mv.onSaveInstanceState(outState); miniMap.onSaveInstanceState(outState); } 

logcat的:

 01-07 08:07:00.548 710-710/com.example.exampleapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.exampleapp, PID: 710 android.view.InflateException: Binary XML file line #13: Error inflating class com.mapbox.mapboxsdk.views.MapView at android.view.LayoutInflater.createView(LayoutInflater.java:633) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at com.example.exampleapp.FragmentMap.onCreateView(FragmentMap.java:133) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) at android.support.v4.view.ViewPager.populate(ViewPager.java:1106) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514) at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495) at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1778) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:871) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:841) at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1115) at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:665) at android.view.View.performClick(View.java:4785) at android.view.View$PerformClick.run(View.java:19884) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:607) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at com.example.exampleapp.FragmentMap.onCreateView(FragmentMap.java:133) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) at android.support.v4.view.ViewPager.populate(ViewPager.java:1106) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514) at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495) at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1778) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:871) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:841) at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1115) at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:665) at android.view.View.performClick(View.java:4785) at android.view.View$PerformClick.run(View.java:19884) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.exampleapp-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "libmapbox-gl.so" at java.lang.Runtime.loadLibrary(Runtime.java:366) at java.lang.System.loadLibrary(System.java:988) at com.mapbox.mapboxsdk.views.NativeMapView.<clinit>(NativeMapView.java:42) at com.mapbox.mapboxsdk.views.MapView.initialize(MapView.java:680) at com.mapbox.mapboxsdk.views.MapView.<init>(MapView.java:621) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:607) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at com.example.exampleapp.FragmentMap.onCreateView(FragmentMap.java:133) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) at android.support.v4.view.ViewPager.populate(ViewPager.java:1106) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514) at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495) at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1778) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:871) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:841) at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1115) at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:665) at android.view.View.performClick(View.java:4785) at android.view.View$PerformClick.run(View.java:19884) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

应用程序gradle:

 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:1.3.0' } } apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' repositories { mavenCentral() jcenter() } android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.example.exampleapp" minSdkVersion 19 targetSdkVersion 23 versionCode 1 versionName "1.0" multiDexEnabled true } useLibrary 'org.apache.http.legacy' buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:cardview-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile 'com.google.android.exoplayer:exoplayer:r1.5.2' compile 'com.google.android.gms:play-services-auth:8.3.0' compile 'com.google.android.gms:play-services-maps:8.3.0' compile('com.mapbox.mapboxsdk:mapbox-android-sdk:2.3.0@aar') { transitive = true } compile files('libs/commons-lang3-3.3.2.jar') } 

项目结构:

在这里输入图像说明

Solutions Collecting From Web of "Mapbox在片段上膨胀视图"

这个库看起来处于繁重的开发状态,可能会有一些问题,这些问题在GitHub上提到过,特别是当您运行版本2.2.0时。

尝试更新版本到最新的稳定:

 compile 'com.mapbox.mapboxsdk:mapbox-android-sdk:3.0.0' 

你在讲通货膨胀,但看看堆栈跟踪:

引起:java.lang.UnsatisfiedLinkError:dalvik.system.PathClassLoader [DexPathList [[zip文件“/data/app/com.example.exampleapp-2/base.apk"],nativeLibraryDirectories=[/vendor/lib,/ system / lib]]] 找不到“libmapbox-gl.so”在java.lang.Runtime.loadLibrary(Runtime.java:366)在java.lang.System.loadLibrary(System.java:988)在com.mapbox .mapbox.views.NativeMapView。(NativeMapView.java:42)at com.mapbox.mapboxsdk.views.MapView.initialize(MapView.java:680)at com.mapbox.mapboxsdk.views.MapView。(MapView.java:621) )

BTW:

第一

你需要决定是否你每次膨胀视图{局部variables)或你重用它(全局一个)

例:

 if(_view==null) _view = inflater.inflate(..); return _view; 

第二:

不要像onCreate onPause手动调用活动或片段方法(由os用来维护生命周期)

第三:

如果你使用viewpager和嵌套片段,你需要使用子片段pipe理器创build适配器

更多和更多:

请记住,要设置一些数据和属性的子片段viewpager片段需要布局

“首先,我每次都在膨胀它,这通常不是问题(我的选项卡与我创build的任何其他片段一起工作,包括Google Maps,而不是Mapbox)第二:是的,您确实手动调用活动生命周期方法。使用onCreate来扩大视图第三:我有一个适配器,我只是没有在这里包括它,这不是问题,我的标签与我创build的任何其他片段正常工作最后:我不知道你“我说,但是我的碎片在我的其他标签中工作得很好。

请专注于手头的问题…将Mapbox视图放入片段中,而不pipe片段在哪里(标签页面查看器)。 – TangoJLabs“

 /** * Called to do initial creation of a fragment. This is called after * {@link #onAttach(Activity)} and before * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}. * * <p>Note that this can be called while the fragment's activity is * still in the process of being created. As such, you can not rely * on things like the activity's content view hierarchy being initialized * at this point. If you want to do work once the activity itself is * created, see {@link #onActivityCreated(Bundle)}. * * @param savedInstanceState If the fragment is being re-created from * a previous saved state, this is the state. */ public void onCreate(@Nullable Bundle savedInstanceState) { mCalled = true; } 

从onCreate片段移动到onViewCreated(View,Bundle)

 mv = (MapView) fragmentLayout.findViewById(R.id.mapview); mv.onCreate(savedInstanceState); miniMap = (MapView) fragmentLayout.findViewById(R.id.mini_map); miniMap.onCreate(savedInstanceState); /** * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} * has returned, but before any saved state has been restored in to the view. * This gives subclasses a chance to initialize themselves once * they know their view hierarchy has been completely created. The fragment's * view hierarchy is not however attached to its parent at this point. * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}. * @param savedInstanceState If non-null, this fragment is being re-constructed * from a previous saved state as given here. */ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { } 

最后一个词:gradle文件

我不知道你的文件内容是否包含gradle文件或只有应用程序文件,但这些行不应该在应用程序gradle文件 – 这些是顶级gradle:

 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:1.3.0' } } repositories { mavenCentral() jcenter() } 

和你应该考虑移动到最新的Android应用程序插件 – 但我看到你正在使用GMS服务 – 所以你顶部的Gradle文件是包含其他插件和上面的那些被省略

按照您的编辑逻辑,我遇到了麻烦。 我没有一个可以移动任何东西的onCreate

 /** inflate fragment - this will create view */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_map, container, false); } /** after view is created - set map view */ @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); if(view!=null) { MapView mv = (MapView) view.findViewById(R.id.mapview); mv.onCreate(savedInstanceState); } } 

顺便说一句我有更多的关注其余的生命周期方法 – 片段可以或不保留状态如此简单的把片段方法中的活动生命周期方法可能会产生更多的问题:)

我不使用&知道MapView的源代码实现,所以我不能告诉你如何实现这一点,但我可以给你提示,以检查是否片段附加到活动之前,你调用任何mapview方法

@ ceph3us -1这里提到的一切都是错误的,你的语法可以改进。 我没有回答这个问题,我在这里也有同样的问题。 第一个根视图不应该是一个字段,每次调用onCreateView时都应该被夸大,第二个视图是正确的调用,第三个和其余的都是主题,对主题没有帮助。 – 海登开

@HaydenKai

  1. 首先请求为你的审议指定来源
  2. 为什么你想重新创build视图? 目的? 浪费资源(如你的时间代码和用户的CPU和MEM)? – 在那里你可以负担得起或需要一个新的一个,那么它确定,但在其他情况下, 重用
  3. 该视图不需要被夸大,它可以正常创build,例如通过返回新的LinearLayout(Context)或FrameLayout或任何其他复杂的视图 – 这是开发人员select使用XML或Java – 我不喜欢使用XML – 我喜欢纯JAVA 🙂
  4. 所以最后我可以说你的-1对我来说是空的

在这里输入图像说明