我有一个 MapActivity 和一个 Spinner。在 Spinner 上有 OnItemSelected 事件,它会改变视图的内容。所以我在微调器 onItemSelected 事件上再次应用了 setContentView,因为我更改了实际包含数据的 var 内容。但在这里它导致了崩溃原因:java.lang.IllegalStateException:您只允许在 MapActivity 中有一个 MapView
所以我这样做了。
startActivity(new Intent(MapMultipleUsersActivity.this,MapMultipleUsersActivity.class));
finish();
我尝试在 mapview 对象上无效,但它的任务不成功。所以我需要做什么呢?
我的完整堆栈跟踪如下
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #6: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:513)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
at android.app.Activity.setContentView(Activity.java:1647)
at com.app.milesoft.phonecash.MapMultipleUsersActivity$2.onItemSelected(MapMultipleUsersActivity.java:93)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
at android.widget.AdapterView.access$200(AdapterView.java:42)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at com.app.milesoft.map.multipleusers.MapLocationViewer.<init>(MapLocationViewer.java:28)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:500)
... 20 more
Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a
MapActivity
at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:379)
at com.google.android.maps.MapView.<init>(MapView.java:280)
at com.google.android.maps.MapView.<init>(MapView.java:226)
at com.app.milesoft.map.multipleusers.MapLocationViewer.init(MapLocationViewer.java:45)
... 24 more
我的 MapActivity xml 如下
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.app.milesoft.map.multipleusers.MapLocationViewer
android:id="@+id/map_location_viewer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
我的 MapLOcation 类如下
public class MapLocationViewer extends LinearLayout {
private MapLocationOverlay overlay;
// Known latitude/longitude coordinates that we'll be using.
private List<MapLocation> mapLocations;
private MapView mapView;
Context mcontext;
public MapLocationViewer(Context context, AttributeSet attrs) {
super(context, attrs);
mcontext=context;
init();
}
public MapLocationViewer(Context context) {
super(context);
init();
}
public void init() {
setOrientation(VERTICAL);
setLayoutParams(new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT));
//this upper row xml is containing the Spinner View
View v=View.inflate(getContext(), R.layout.map_upper_row, null);
//mapView = new MapView(getContext(),"0DUEIIn35xtmfWC2DXprK5kqNF-aEaNgRJ4ONxw");
//Change Your Map Key Here.....
mapView = new MapView(getContext(),"0YOGrMpIJgaO92C0lXFtI6qF5YRl2Oe5h9UVoiQ");
mapView.setEnabled(true);
mapView.setClickable(true);
addView(v);
addView(mapView);
overlay = new MapLocationOverlay(this);
mapView.getOverlays().add(overlay);
mapView.getController().setZoom(14);
//mapView.setSatellite(true);
mapView.getController().setCenter(getMapLocations().get(0).getPoint());
}
}