Fen*_*nil 8 xml android listview android-layout google-maps-android-api-2
我正在尝试制作一个应用,当用户点击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));
}
}
Run Code Online (Sandbox Code Playgroud)
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" />
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我想在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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
在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;
}
Run Code Online (Sandbox Code Playgroud)
当我第一次点击ListView的项目时,它会显示Map ...然后,当我选择另一个项目时,它会崩溃:
Logcat日志:
android.view.InflateException:二进制XML文件行#7:在android.view.LayoutInflater.rInflate(LayoutInflater.java:739)的android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)中填充类片段的错误. 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.Handler.dispatchMessage(Handler.java:92)的android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:426)android.os.Handler.handleCallback(Handler.java:605)在android.app.Looper.loop(Looper.java:137)android.app.ActivityThread.main(ActivityThread.java:44 24)在java.lang.reflect.Method.invokeNative(本机方法)在java.lang.reflect.Method.invoke(Method.java:511)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit. java:825)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)at dalvik.system.NativeStart.main(Native Method)引起:java.lang.IllegalArgumentException:二进制XML文件行#7 :在android.view.LayoutInflater.createViewFromTag上的android.app.Activity.onCreateView(Activity.java:4248)上的com.google.android.gms.maps.MapFragment的另一个片段重复ID 0x7f0b001e,标记为null或父ID为0xffffffff (LayoutInflater.java:673)在android.view.LayoutInflater.rInflate(LayoutInflater.java:739)在android.view.LayoutInflater.inflate(LayoutInflater.java:489)在android.view.LayoutInflater.inflate(LayoutInflater.java: 396)在android.app.FragmentManagerI的com.krupal.rememberthisplace.MapsFragment.onCreateView(MapsFragment.java:40)mpl.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.Handler.dispatchMessage(Handler.java:92)android.app.Handler.handleCallback(Handler.java:605)的android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:426) )在android.app.Looper.loop(Looper.java:137)的android.app.ActivityThread.main(ActivityThread.java:4424),位于java.lang的java.lang.reflect.Method.invokeNative(Native Method). 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)
编辑: 这个解决方案对我不起作用
Fen*_*nil 24
我现在解决了!
另一个解决方案是链接的答案对我有用.
正如那里所解释的,我添加onDestroyView()了包含地图的片段.
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.mymap);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14699 次 |
| 最近记录: |