Android fragments 以编程方式添加MapBox
我试图以编程方式添加一个映射,作为一个片段。不幸的是,我没有取得多大成功。我还试图从本地存储的GeoJSTON文件中添加标记 以下是我的主要活动:Android fragments 以编程方式添加MapBox,android-fragments,mapbox,programmatically-created,Android Fragments,Mapbox,Programmatically Created,我试图以编程方式添加一个映射,作为一个片段。不幸的是,我没有取得多大成功。我还试图从本地存储的GeoJSTON文件中添加标记 以下是我的主要活动: public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConte
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//add a fragment
MapActivity myFragment = new MapActivity();
fragmentTransaction.add(R.id.FragmentContainer, myFragment);
fragmentTransaction.commit();
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/FragmentContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</FrameLayout>
</LinearLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.views.MapView
android:id="@+id/mapview"
android:layout_height="match_parent"
android:layout_width="match_parent"
mapbox:mapid="@string/mapLisbon"
mapbox:accessToken="@string/testAccessToken"/>
</FrameLayout>
以下是主要活动的XML:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//add a fragment
MapActivity myFragment = new MapActivity();
fragmentTransaction.add(R.id.FragmentContainer, myFragment);
fragmentTransaction.commit();
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/FragmentContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</FrameLayout>
</LinearLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.views.MapView
android:id="@+id/mapview"
android:layout_height="match_parent"
android:layout_width="match_parent"
mapbox:mapid="@string/mapLisbon"
mapbox:accessToken="@string/testAccessToken"/>
</FrameLayout>
根据堆栈跟踪,问题似乎是在运行时类路径上找不到lisbon.geojson。它是否位于
src/main/assets
目录中?非常感谢您抽出时间回答。事实上,当我最初发布这个问题时,lisbon.geojson不在正确的位置,但它现在位于资产目录中。然而,我一直得到的是一个底部有一个后退按钮的空白用户界面。您可能想尝试对@+id/FragmentContainer
使用FrameLayout
而不是LinearLayout
,不幸的是,运气还不好。我已经更新了上面的错误/警告日志,也许这有助于发现问题。