簡體   English   中英

Mapbox在片段上的膨脹視圖

[英]Mapbox Inflate View on Fragment

我有問題在Android片段上膨脹Mapbox MapView。 當我在Activity上使用相同的代碼時(除了Activity和Fragment之間的標准差異),我可以正確加載MapView,但不能在Fragment上加載。

Mapbox 在這里也有一個MapFragment類,但我不確定如何在Fragment中正確使用它(該示例仍然在Activity中); 使用Google Maps 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)

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

項目結構:

在此輸入圖像描述

你講的是通貨膨脹,但看看堆棧追蹤:

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

BTW:

第一

你需要決定你是否每次{local variable}或者你重用它(全局一個)來膨脹視圖

例:

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

第二:

不要手動調用onCreate onPause等活動或片段方法(os用於維護生命周期的方法)

第三:

如果您使用viewpager和嵌套片段,則需要使用子片段管理器創建適配器

更多:

請記住,要在子片段中設置一些數據和屬性viewpager片段需要布局

“首先:我每次都在膨脹,這通常不是問題(我的標簽可以與我創建的任何其他片段一起使用,包括谷歌地圖,而不是Mapbox)第二:是的,你手動調用活動生命周期方法。你有使用onCreate來擴展視圖。第三:我有一個適配器,我只是沒有在這里包含它。這不是問題,我的標簽可以與我創建的任何其他片段一起工作。最后:我不確定你是什么我說,但我的片段在我的其他標簽中運行良好。

請關注手頭的問題......將一個Mapbox視圖放在一個片段中,>無論該片段在哪里(標簽頁面瀏覽器與否)... - 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 of fragment移到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文件還是只包含app文件,但這些行不應該在app 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方法之前檢查片段是否附加到activity

@ ceph3us -1這里提到的一切都是錯的,你的語法可以改進。 我對此沒有答案,我在這里也有同樣的問題。 第一個根視圖不應該是一個字段,它應該在每次調用onCreateView時膨脹,第二個是正確的調用,第三個,其余的是主題,對主題沒有幫助。 - 海登凱

@HaydenKai

  1. 首先請求為您審議的來源
  2. 為什么你想重新創建視圖? 目的? 浪費資源(比如你的代碼和用戶CPU和MEM的時間)? - 在那里你可以負擔得起或需要一個新的,然后它的確定,但在其他情況下重新使用
  3. 視圖根本不需要膨脹,它通常可以通過返回新的LinearLayout(Context)或FrameLayout或任何其他復雜視圖來創建 - 這是開發人員選擇使用xml或java - 我不想使用xml - 我喜歡純JAVA :)
  4. 所以在最后我可以說你的-1對我來說是空的

在此輸入圖像描述

這個庫看起來處於繁重的開發狀態,並且可能存在一些問題,這些問題在GitHub上提到,尤其是在運行2.2.0版本時。

嘗試將版本更改為最新穩定版:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM