gpt4 book ai didi

java - fragment 上的 Mapbox 膨胀 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:11:51 26 4
gpt4 key购买 nike

我在为 Android Fragment 上的 Mapbox MapView 充气时遇到问题。当我在 Activity 上使用相同的代码时(除了 Activity 和 Fragment 之间的标准差异),我可以让 MapView 正确加载,但不能在 Fragment 上加载。

Mapbox 也有一个 MapFragment 类 here ,但我不确定如何在 fragment 中正确使用它(该示例仍在 Activity 中);借助 Google Maps API,我可以毫无问题地在 Fragment 上使用 MapView。

下面的 logcat 行:

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

指的是 Java 行:

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

这里是这个问题的相关代码和日志:

fragment 映射.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>

fragment 映射.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);
}

日志:

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)

应用程序等级:

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')
}

项目结构:

enter image description here

最佳答案

你说的是通货膨胀,但看看堆栈跟踪:

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.(NativeMapView.java:42) at com.mapbox.mapboxsdk.views.MapView.initialize(MapView.java:680) at com.mapbox.mapboxsdk.views.MapView.(MapView.java:621)

顺便说一句:

首先

你需要决定是每次都膨胀 View (局部变量)还是重用它(全局变量)

例子:

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

第二个:

不要手动调用onCreate onPause之类的activity或fragment方法(那些被os用来维护生命周期的方法)

第三:

如果您使用 viewpager 和嵌套 fragment ,您需要使用子 fragment 管理器创建适配器

更多更多:

请记住,要在子 fragment viewpager fragment 中设置一些数据和属性需要布局

"First: I am inflating it every time, which is usually not a problem (my tabs work with any other fragment I have created, including Google Maps, just not Mapbox) Second: Yes, you do call activity lifecycle methods manually. You have to use onCreate to inflate a view. Third: I have an adapter, I just did not include it here. That is not the issue, my tabs work fine with any other fragment I have created. Lastly: I'm not sure what you're saying, but my fragments work fine in my other tabs.

Please focus on the problem at hand... putting a Mapbox view in a fragment, > regardless of where that fragment is (tab pageviewer or not).. – 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;
}

将其从 fragment 的 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 文件包含其他插件,上面的插件被省略了

I'm having trouble following the logic of your edits. I don't have an onCreate from which I can move anything

/** 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);
}
}

顺便说一句,我对其余的生命周期方法有更多的担忧—— fragment 可能会或不会保留状态,所以简单地将 Activity 生命周期方法放在 fragment 方法中可能会产生更多问题:)

我不使用也不了解 MapView 的源代码实现,所以我无法告诉您如何实现它,但我可以给您一个提示,让您在调用任何 mapview 方法之前检查 fragment 是否附加到 Activity 上

@ceph3us -1 everything mentioned in this is wrong, and your grammar can be improved. I don't have an answer to this, I am here with the same question. 1st root view should not be a field, it should be inflated on each call to onCreateView, 2nd those are proper calls, 3rd and the rest are off topic and not helpful to the subject. – HaydenKai

@海登凯

  1. 首先请为您的审议指定SOURCES
  2. 为什么要重新创建 View ?目的 ?浪费资源(比如你的代码和用户 CPU 和 MEM 的时间)? - 在那里你买得起或需要一个新的然后没关系,但在其他情况下 REUSE !!!
  3. View 根本不需要膨胀,它通常可以通过 return new LinearLayout(Context) 或 FrameLayout 或任何其他复杂 View 创建 - 这是开发人员选择使用 xml 或 java - 我不想这样做使用 xml - 我喜欢纯 JAVA :)
  4. 所以最后我可以说你的 -1 对我来说是空的她

enter image description here

关于java - fragment 上的 Mapbox 膨胀 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34666700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com