我正在尝试创建一个应用,当用户点击ListView中的项目时,会显示GoogleMaps。
我尝试过以下代码:
ShowPlacesActivity.java
public class ShowPlaceActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_place);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container_showplaces, new PlaceholderFragment())
.commit();
}
getActionBar().setSubtitle(getString(R.string.activity_showplaces_subtitle));
}
}
activity_show_place.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container_showplaces"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.krupal.rememberthisplace.ShowPlaceActivity" tools:ignore="MergeRootFrame" />
fragment_show_place.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/listView_places"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
fragment_maps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/mymap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
我想在listview的项目上显示地图点击...所以,我已经实现了两个片段如下:
在 PlaceholderFragment.java 中,我将onitemclicklistener
设置为:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_show_place, container, false);
final MySqliteHelper db = new MySqliteHelper(getActivity().getBaseContext());
listview_places = (ListView)rootView.findViewById(R.id.listView_places);
final PlacesAdapter places_adapter = new PlacesAdapter(getActivity(),R.layout.listview_item_row,places);
listview_places.setAdapter(places_adapter);
listview_places.setLongClickable(true);
listview_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position,long id) {
Place place = places_adapter.getItem(position);
String dbvaluename = place.getname();
Place selectedplace = db.getPlace(dbvaluename);
dblatitude = selectedplace.getlatitude();
dblongitude = selectedplace.getlongitude();
dbplacename = place.getname();
maps = new MapsFragment();
Bundle bundle = new Bundle();
bundle.putDouble("dbplacename",dblatitude);
bundle.putDouble("dbplacename",dblongitude);
bundle.putString("dbplacename",dbplacename);
maps.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_showplaces,maps);
ft.addToBackStack(null);
ft.commit();
}
});
}
}
并在 MapsFragment.java 中,我将地图显示为:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_maps, container, false);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
当我第一次点击ListView的项目时,它会显示Map ...然后,当我选择另一个项目时,它会崩溃:
Logcat日志:
android.view.InflateException:二进制XML文件行#7:错误 膨胀类片段 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 在android.view.LayoutInflater.inflate(LayoutInflater.java:489) 在android.view.LayoutInflater.inflate(LayoutInflater.java:396) 在com.krupal.rememberthisplace.MapsFragment.onCreateView(MapsFragment.java:40) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) 在android.app.BackStackRecord.run(BackStackRecord.java:622) 在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) 在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:426) 在android.os.Handler.handleCallback(Handler.java:605) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592) at dalvik.system.NativeStart.main(Native Method)引起:java.lang.IllegalArgumentException:二进制XML文件行#7:重复 id 0x7f0b001e,tag null或父id为0xffffffff,带有另一个片段 对于com.google.android.gms.maps.MapFragment 在android.app.Activity.onCreateView(Activity.java:4248) 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 在android.view.LayoutInflater.inflate(LayoutInflater.java:489) 在android.view.LayoutInflater.inflate(LayoutInflater.java:396) 在com.krupal.rememberthisplace.MapsFragment.onCreateView(MapsFragment.java:40) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) 在android.app.BackStackRecord.run(BackStackRecord.java:622) 在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) 在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:426) 在android.os.Handler.handleCallback(Handler.java:605) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592) 在dalvik.system.NativeStart.main(本地方法)
编辑: this soloution对我不起作用
答案 0 :(得分:22)
我现在解决了!
另一个解决方案是链接的答案对我有用。
https://stackoverflow.com/a/14484640/3960528
正如那里所解释的那样,我在包含地图的片段中添加了onDestroyView()
。
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.mymap);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
答案 1 :(得分:3)
这解决了我的问题:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(rootView==null){
rootView = inflater.inflate(R.layout.MapFragment, container, false);
}
return rootView;
}
更多信息: https://colinyeoh.wordpress.com/2014/07/20/android-mapfragment-exception-when-clicked-twice/
答案 2 :(得分:0)
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.mymap);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
实际上,您不需要在onDestroyView中使用它。我的情况是使用SupportMapFragment删除膨胀的布局,就是这样。