我有一个菜单项,当你点击它时会显示地图。当我单击此菜单项两次时,它会出现以下错误:
12-31 12:32:13.569: E/AndroidRuntime(4318): Process: com.example.makemyday, PID: 4318
12-31 12:32:13.569: E/AndroidRuntime(4318): android.view.InflateException: Binary XML file line #21: Error inflating class fragment
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-31 12:32:13.569: E/AndroidRuntime(4318): at com.example.makemyday.MapFragment.onCreateView(MapFragment.java:24)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.os.Handler.handleCallback(Handler.java:733)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.os.Handler.dispatchMessage(Handler.java:95)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.os.Looper.loop(Looper.java:136)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-31 12:32:13.569: E/AndroidRuntime(4318): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 12:32:13.569: E/AndroidRuntime(4318): at java.lang.reflect.Method.invoke(Method.java:515)
12-31 12:32:13.569: E/AndroidRuntime(4318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-31 12:32:13.569: E/AndroidRuntime(4318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-31 12:32:13.569: E/AndroidRuntime(4318): at dalvik.system.NativeStart.main(Native Method)
12-31 12:32:13.569: E/AndroidRuntime(4318): Caused by: java.lang.IllegalArgumentException: Binary XML file line #21: Duplicate id 0x7f050028, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
12-31 12:32:13.569: E/AndroidRuntime(4318): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
12-31 12:32:13.569: E/AndroidRuntime(4318): ... 20 more
这是我的MapFragment代码:
package com.example.makemyday;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class MapFragment extends Fragment implements OnItemSelectedListener{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_map_view, container, false);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
DateTimeFragment datetime=new DateTimeFragment();
ft.add(R.id.datetime_container_map, datetime);
SupportMapFragment mapFragment=null;
if(mapFragment==null)
mapFragment= (SupportMapFragment)fm.findFragmentById(R.id.map_fragment);
ft.commit();
AutoCompleteTextView location= (AutoCompleteTextView)v.findViewById(R.id.location_map);
location.setAdapter(new PlacesAutoCompleteAdapter(getActivity(),R.layout.list_layout));
Spinner category=(Spinner)v.findViewById(R.id.category_query_map);
ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(this.getActivity().getBaseContext(),R.array.category_names, android.R.layout.simple_spinner_item);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(adapterCategory);
category.setOnItemSelectedListener(this);
return v;
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
这是我的地图片段布局代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<AutoCompleteTextView
android:id="@+id/location_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:background="@color/translucent_grey"
android:ems="10"
android:hint="@string/hint_location"
android:inputType="text"
android:textSize="14sp" >
<requestFocus />
</AutoCompleteTextView>
<fragment
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="300dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<FrameLayout
android:id="@+id/datetime_container_map"
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_weight="0.00" />
<Spinner
android:id="@+id/category_query_map"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:textSize="14sp" />
</LinearLayout>
</ScrollView>
为什么第一次创建地图片段时没有异常,但是如果我第二次创建地图片段则会出现异常?
答案 0 :(得分:1)
例如下面的FragmentManager使用。
mapFragment= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map_fragment);
答案 1 :(得分:0)
尝试更改此
FragmentManager fm = getFragmentManager();
到
FragmentManager fm = getSupportFragmentManager();
内部片段使用此
FragmentManager fm = getActivity().getSupportFragmentManager();
另外
private GoogleMap map;
....
map = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap();
答案 2 :(得分:0)
尝试如下:使用getMap()
加载地图。
if(mapFragment==null)
mapFragment= (SupportMapFragment)fm.findFragmentById(R.id.map_fragment).getMap();