2012-10-06 110 views
0

我想在scrollView中使用MapView。這樣做會導致地圖出現滾動問題,並且當您想要滾動地圖時,整個頁面將滾動。我在這裏發現了這個問題的解決方案:MapView inside a ScrollView?
我創建了一個名爲myMapView的類。這裏是它的代碼:
覆蓋MapView中的onTouchEvent

package com.wikitude.example; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 

import com.google.android.maps.MapView; 

public class myMapView extends MapView { 

    public myMapView(Context context, String apiKey) { 
     super(context, apiKey); 
     // TODO Auto-generated constructor stub 
    } 

    public myMapView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    public myMapView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     int action = ev.getAction(); 
     switch (action) { 
     case MotionEvent.ACTION_DOWN: 
      // Disallow ScrollView to intercept touch events. 
      this.getParent().requestDisallowInterceptTouchEvent(true); 
      break; 

     case MotionEvent.ACTION_UP: 
      // Allow ScrollView to intercept touch events. 
      this.getParent().requestDisallowInterceptTouchEvent(false); 
      break; 
     } 

     // Handle MapView's touch events. 
     super.onTouchEvent(ev); 
     return false; 
    } 
} 

,但是當我嘗試使用它在我的MapActivity這樣的:

myMapView myview = (myMapView) findViewById(R.id.themap); 

它拋出這個錯誤:
Undable to start activity ComponentInfo{com.smtabatabaie.example/com.smtabatabaie.mainActivity}: java.lang.ClassCastException: com.google.android.maps.MapView
我沒有找到問題所在,看起來一切正常。我會不勝感激,如果有人可以幫我這個
謝謝

+0

它是什麼錯誤投擲,請張貼您的問題的Logcat錯誤日誌。 –

+0

感謝Vishwa,我編輯了我的問題併發布了錯誤 – m0j1

回答

3

這就是爲什麼你會得到這種ClassCastException。在您聲明自定義mapview的XML文件中,您必須實際聲明自定義mapview的名稱,以便在您的情況下它將是myMapView。這是你的XML文件應該是這樣的:

<com.wikitude.example.myMapView //This is where you're probably going wrong (so what I've posted is the right way to declare it) 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mapview" 
android:layout_width="fill_parent" //Replace these with whatever width and height you need 
android:layout_height="fill_parent" 
android:clickable="true" 
android:apiKey="Enter-your-key-here" 
/> 
+0

謝謝,那正是導致錯誤的問題。非常感謝Vishwa;) – m0j1