Show MapView on PopupWindow
- by Ali Nadi
I want to show MapView on PopupWindow and get error when press-on Map.
Please help!
Liste.java
MapView mapView;
View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
LayoutInflater mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.pop_up, (ViewGroup) findViewById(R.id.popup_element), false);
mapView = (MapView) view.findViewById(R.id.mapview_popup);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
initiatePopupWindow(earthQuakeList.get(position));
}
});
}
private void initiatePopupWindow(EqData data) {
try {
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable marker = this.getResources().getDrawable(R.drawable.marker1);
HaritaOverlay itemizedoverlay = new HaritaOverlay(marker, this);
Coordination coord = data.getCoordination();
GeoPoint point = new GeoPoint( (int)coord.latitude, (int)coord.longitude);
OverlayItem overlayitem = new OverlayItem(point, data.lokasyon, data.name);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
Display display = getWindowManager().getDefaultDisplay();
pw = new PopupWindow(view, display.getWidth(), display.getHeight()/2, true);
// display the popup in the center
pw.showAtLocation(view, Gravity.CENTER, 0, display.getHeight()/2);
} catch (Exception e) {
e.printStackTrace();
}
}
pop_up.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:weightSum="540"
android:id="@+id/popup_element" >
...
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="440" >
<com.google.android.maps.MapView
android:id="@+id/mapview_popup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="@string/ApiMapKey"
android:clickable="true" />
</LinearLayout>
Error
07-23 17:36:28.820: E/MapActivity(12413): Couldn't get connection factory client
07-23 17:36:37.760: E/AndroidRuntime(12413): FATAL EXCEPTION: main
07-23 17:36:37.760: E/AndroidRuntime(12413): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W@40590b70 is not valid; is your activity running?
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.ViewRoot.setView(ViewRoot.java:528)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.Window$LocalWindowManager.addView(Window.java:465)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.widget.ZoomButtonsController.setVisible(ZoomButtonsController.java:370)
07-23 17:36:37.760: E/AndroidRuntime(12413): at com.google.android.maps.MapView.displayZoomControls(MapView.java:1053)
07-23 17:36:37.760: E/AndroidRuntime(12413): at com.google.android.maps.MapView$1.onDown(MapView.java:341)
07-23 17:36:37.760: E/AndroidRuntime(12413): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:488)
07-23 17:36:37.760: E/AndroidRuntime(12413): at com.google.android.maps.MapView.onTouchEvent(MapView.java:683)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.View.dispatchTouchEvent(View.java:3901)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2200)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.view.ViewRoot.handleMessage(ViewRoot.java:1884)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.os.Looper.loop(Looper.java:130)
07-23 17:36:37.760: E/AndroidRuntime(12413): at android.app.ActivityThread.main(ActivityThread.java:3835)
07-23 17:36:37.760: E/AndroidRuntime(12413): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 17:36:37.760: E/AndroidRuntime(12413): at java.lang.reflect.Method.invoke(Method.java:507)
07-23 17:36:37.760: E/AndroidRuntime(12413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
07-23 17:36:37.760: E/AndroidRuntime(12413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-23 17:36:37.760: E/AndroidRuntime(12413): at dalvik.system.NativeStart.main(Native Method)