我的 android 平板电脑应用程序中有两个窗格(左侧列表和右侧详细信息)(就像设置本机平板电脑应用程序视图一样)。我已将 MapActivity 扩展到我的 MainActivity。我在左侧有一个位置详细信息列表。我想在我的右窗格中显示每个列表项的 MapView,这是一个片段。
问题是当我尝试打开 Map View 时,第一个项目是打开 mapView。从第二项开始,它显示您只能为一个 MapActivity 打开一个 MapView。请提出正确答案。我已经尝试了所有相关的帖子,但没有成功。
我的代码版本:android 4.0
请检查以下代码:
MainActivity extends MapActivity {
onCreate(){
LocationFragment loc_fragment=new LocationFragment();
FragmentTransaction xaction=VenueFragment.venueFrgMgr.beginTransaction();
Bundle sendData=new Bundle();
sendData.putString("LAT", s.getLat());
sendData.putString("LON", s.getLong());
sendData.putString("NAME", s.getName());
sendData.putString("ADD", s.getAddress());
loc_fragment.setArguments(sendData);
xaction.add(R.id.second_pane, loc_fragment,"location");
xaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
xaction.addToBackStack(null);
xaction.commit();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
显示 MapView 的 LocationFragment 代码如下:
private ViewGroup mapContainer;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//view.refreshDrawableState();
view = inflater.inflate(R.layout.location, container, false);
mapContainer=new LinearLayout(getActivity());
mapContainer.addView(view);
v = view.findViewById(R.id.loading);
v.setVisibility(View.VISIBLE);
/* lonstr=LocationActivity.lonstr;
latstr=LocationActivity.latstr;
name=LocationActivity.name;
address=LocationActivity.address;
*/
((Button) view.findViewById(R.id.back))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//getActivity().finish();
if(mapView != null) {
mapView.removeAllViewsInLayout();
if(mapContainer!=null)
mapContainer.removeView(mapView);
RelativeLayout parentView = (RelativeLayout) mapView.getParent();
//mapView.removeAllViews();
((MapView)view.findViewById(R.id.mapview)).removeAllViews();
parentView.removeAllViews();
if(parentView!=null)
parentView.recomputeViewAttributes(mapView);
getFragmentManager().findFragmentByTag("location").getView().destroyDrawingCache();
}
getFragmentManager().popBackStack();
// getFragmentManager().popBackStackImmediate();
}
});
mapView = (MapView) view.findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
Bundle b = this.getArguments();
if (b != null) {
latstr = b.getString("LAT");
lonstr = b.getString("LON");
name = b.getString("NAME");
address = b.getString("ADD");
}
String coordinates[] = { latstr, lonstr };
try {
lat = Double.parseDouble(coordinates[0]);
lng = Double.parseDouble(coordinates[1]);
} catch (Exception e) {
// TODO: handle exception
}
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
Drawable icon = getResources().getDrawable(R.drawable.red_pin);
icon.setBounds(0, 0, icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());
MyItemizedOverlay overlay = new MyItemizedOverlay(icon);
OverlayItem item = new OverlayItem(p, "", null);
overlay.addItem(item);
overlay.populateNow();
mapView.getOverlays().add(overlay);
mapView.postInvalidate();
mc.setZoom(12);
if (name != null)
((TextView) view.findViewById(R.id.event_name)).setText(name);
if (address != null)
((TextView) view.findViewById(R.id.event_date)).setText(address);
getWheather();
return mapContainer;
}
我得到的错误如下:
10-15 10:43:03.881: E/AndroidRuntime(3778): android.view.InflateException: Binary XML file line #184: Error inflating class <unknown>
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.view.LayoutInflater.createView(LayoutInflater.java:606)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-15 10:43:03.881: E/AndroidRuntime(3778): at webspiders.event2mobile.asme.LocationFragment.onCreateView(LocationFragment.java:128)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.app.BackStackRecord.run(BackStackRecord.java:622)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.os.Handler.handleCallback(Handler.java:605)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.os.Looper.loop(Looper.java:137)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.app.ActivityThread.main(ActivityThread.java:4507)
10-15 10:43:03.881: E/AndroidRuntime(3778): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 10:43:03.881: E/AndroidRuntime(3778): at java.lang.reflect.Method.invoke(Method.java:511)
10-15 10:43:03.881: E/AndroidRuntime(3778): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
10-15 10:43:03.881: E/AndroidRuntime(3778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
10-15 10:43:03.881: E/AndroidRuntime(3778): at dalvik.system.NativeStart.main(Native Method)
10-15 10:43:03.881: E/AndroidRuntime(3778): Caused by: java.lang.reflect.InvocationTargetException
10-15 10:43:03.881: E/AndroidRuntime(3778): at java.lang.reflect.Constructor.constructNative(Native Method)
10-15 10:43:03.881: E/AndroidRuntime(3778): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-15 10:43:03.881: E/AndroidRuntime(3778): at android.view.LayoutInflater.createView(LayoutInflater.java:586)
10-15 10:43:03.881: E/AndroidRuntime(3778): ... 19 more
10-15 10:43:03.881: E/AndroidRuntime(3778): Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
10-15 10:43:03.881: E/AndroidRuntime(3778): at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:397)
10-15 10:43:03.881: E/AndroidRuntime(3778): at com.google.android.maps.MapView.<init>(MapView.java:289)
10-15 10:43:03.881: E/AndroidRuntime(3778): at com.google.android.maps.MapView.<init>(MapView.java:264)
10-15 10:43:03.881: E/AndroidRuntime(3778): at com.google.android.maps.MapView.<init>(MapView.java:247)