Android-支持Google地图API 2.0的MapFragment,提供IllegalArgumentException
我正在尝试使用为Android提供的最新Map API 2.0。我正在使用支持库,因为我想支持Android 2.2。以下是我的代码: 主要活动类Android-支持Google地图API 2.0的MapFragment,提供IllegalArgumentException,android,maps,android-fragments,google-maps-api-2,supportmapfragment,Android,Maps,Android Fragments,Google Maps Api 2,Supportmapfragment,我正在尝试使用为Android提供的最新Map API 2.0。我正在使用支持库,因为我想支持Android 2.2。以下是我的代码: 主要活动类 public class MainActivity extends FragmentActivity { public FragmentManager fManager ; @Override protected void onCreate(Bundle savedInstanceState) { supe
public class MainActivity extends FragmentActivity {
public FragmentManager fManager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fManager = getSupportFragmentManager();
Button showMapButton = (Button) findViewById(R.id.showMapButton);
showMapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loadMapFragment();
}
});
}
private void loadMapFragment() {
MapPageFragment plotterFragment = new MapPageFragment();
FragmentTransaction ft = fManager.beginTransaction();
ft.replace(R.id.allFragmentsFrameLayout, plotterFragment);
ft.addToBackStack(null);
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
public class MapPageFragment extends SupportMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.map_fragment_layout, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
public class MapPageFragment extends SupportMapFragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = super.onCreateView(inflater, container, savedInstanceState);
if (rootView == null)
{
rootView = inflater.inflate(R.layout.map_fragment_layout, container, false);
}
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
}
}
主要活动布局文件
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/showMapButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show Map"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:layout_alignParentTop="true" />
<FrameLayout
android:id="@+id/allFragmentsFrameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true">
<!-- Put fragments dynamically -->
</FrameLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapfragmentexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="com.plotter.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.plotter.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyA5FtIeLQ1gGUihZIZPQVi3Yz_0l4NG9PY"/>
</application>
</manifest>
地图片段布局
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:uiCompass="true"
map:mapType= "normal"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true" />
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
map:uiCompass="true"
map:mapType= "normal"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true"
tools:context="the.package.name.of.mappagefragment.MapPageFragment" />
我没有弄错或遗漏任何东西。如果删除
onDestroyView()中的所有嵌套片段,您可以修复此问题。不知道这是不是一个合适的解决方案
public void onDestroyView() {
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
并像往常一样在onCreateView()中对其进行充气
我在《杰克·沃顿视频指南》和《夏洛克碎片》中也面临着同样的问题。这是一个已解决问题的示例TestFragment,感谢您提供上述解决方案。你的碎片可能和我的有点不同
参考:
祝你好运
我也有同样的问题。尝试添加
android:name="com.mapfragmentexample.MapPageFragment"
。。。到布局文件中的片段
您需要使用getChildFragmentManager()来添加SupportMapFragment,而不是通过xml
有关“原因”,请参见官方文件:
看看我在这里的回答:当一个布局包含一个片段时,您不能将该布局膨胀为一个片段。嵌套片段仅在动态添加到片段时才受支持 使用此解决方案(与其他类似):
在使用if(fragment.isResumed)之前,我遇到了问题。在您的活动的onCreate()
中使用dialog.SetContentView()
方法,因为我们尝试加载时
对话框它再次只加载对话框,而不是整个活动生命周期,并导致它出现重复id的异常
试试看 试试这个
映射片段布局
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:uiCompass="true"
map:mapType= "normal"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true" />
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
map:uiCompass="true"
map:mapType= "normal"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true"
tools:context="the.package.name.of.mappagefragment.MapPageFragment" />
我使用Natalia response,但有时会破坏应用程序。
使用这一个,它工作完美,没有打破
@Override
public void onDestroyView() {
try{
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.remove(nestedFragment);
transaction.commit();
}catch(Exception e){
}
super.onDestroyView();
}
使用此选项可以节省时间。使用意图标志:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
调用主活动时。您的问题解决了吗?我也面临同样的问题thing@Natalia我也有同样的问题…如果你能解决,请告诉我it@Hades我贴了一张answer@Natalia我认为你不应该使用:)@Hades,如果你知道更好的方法,我会非常感激。是的,这是正确的解决方案,在《夏洛克碎片》中与JW ViewPagerIndicator一起为我工作。谢谢。除非你在做后台导航和从后台加载片段,否则这一切都很好。每次按下“后退”按钮时,我都会添加“hack”来执行上述操作。add ft.commitAllowingStateLoss();如果你还想处理方向。这不是一个合适的解决方案。@jasoncross通过代码创建片段。请看下面我的答案。我目前正在使用Natalia的解决方案,我想尝试一下你的建议。谢谢
@Override
public void onDestroyView() {
try{
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.remove(nestedFragment);
transaction.commit();
}catch(Exception e){
}
super.onDestroyView();
}
Fragment fragment = (getChildFragmentManager().findFragmentById(R.id.mapview));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);