问题是我在滑块中有三个列表,它们调用三个不同的片段,其中一个片段包含谷歌地图。如果我第一次打开它,地图会完美显示,但第二次更改标签时会抛出错误
08-29 14:29:28.409: E/AndroidRuntime(5543): at savp.rebualo.tootle.Dashboard$PlaceholderFragment.onCreateView(Dashboard.java:153)
08-29 14:29:28.409: E/AndroidRuntime(5543): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
08-29 14:29:28.409: E/AndroidRuntime(5543): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
08-29 14:29:28.409: E/AndroidRuntime(5543): at android.app.BackStackRecord.run(BackStackRecord.java:635)
08-29 14:29:28.409: E/AndroidRuntime(5543): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
08-29 14:29:28.409: E/AndroidRuntime(5543): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
08-29 14:29:28.409: E/AndroidRuntime(5543): at android.os.Handler.handleCallback(Handler.java:615)
08-29 14:29:28.409: E/AndroidRuntime(5543): at android.os.Handler.dispatchMessage(Handler.java:92)
08-29 14:29:28.409: E/AndroidRuntime(5543): at android.os.Looper.loop(Looper.java:137)
08-29 14:29:28.409: E/AndroidRuntime(5543): at android.app.ActivityThread.main(ActivityThread.java:4960)
08-29 14:29:28.409: E/AndroidRuntime(5543): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 14:29:28.409: E/AndroidRuntime(5543): at java.lang.reflect.Method.invoke(Method.java:511)
08-29 14:29:28.409: E/AndroidRuntime(5543): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
08-29 14:29:28.409: E/AndroidRuntime(5543): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-29 14:29:28.409: E/AndroidRuntime(5543): at dalvik.system.NativeStart.main(Native Method)
08-29 14:29:28.409: E/AndroidRuntime(5543): Caused by: java.lang.IllegalArgumentException: Binary XML file line #29: Duplicate id 0x7f080084, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
这是xml布局,我点击列表这个xml包含谷歌地图的片段
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="false"
android:layout_margin="10dp"
android:orientation="vertical" >
<EditText
android:id="@+id/source_l"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="@string/source_l" />
<EditText
android:id="@+id/source_d"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="@string/source_d" />
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="2.57" />
</LinearLayout>
</RelativeLayout>
这就是我初始化谷歌地图的方式
public class AddRide extends Fragment {
private GoogleMap googleMap;
private View rootView;
public static AddRide newInstance() {
AddRide fragment = new AddRide();
return fragment;
}
public AddRide() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.layout_add_ride, container, false);
initilizeMap();
} catch (Exception e) {
/* map is already there, just return view as it is */
}
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((Dashboard) activity).onSectionAttached(1);
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity(),"Text!",Toast.LENGTH_SHORT).show();
}
}
}
}
这就是我将fragents添加到我的活动中的方式。
public class Dashboard扩展了Activity实现 NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in
* {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container,
PlaceholderFragment.newInstance(position + 1)).commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.dashboard, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView=null;
switch(getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
rootView = inflater.inflate(R.layout.layout_searchride, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.layout_add_ride, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.layout_ride_notification, container, false);
break;
}
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((Dashboard) activity).onSectionAttached(getArguments().getInt(
ARG_SECTION_NUMBER));
}
}
}
答案 0 :(得分:0)
尝试将android:id="@+id/map"
更改为
<fragment
android:id="@+id/myGoogleMap"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="2.57" />
并在initilizeMap
googleMap = ((MapFragment) getChildFragmentManager().findFragmentById(R.id.myGoogleMap)).getMap();