2012-10-06 58 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