0

我想在 scrollView 中使用 MapView 。这样做会导致您的地图出现滚动问题,当您想要滚动地图时,整个页面都会滚动。我在这里找到了解决这个问题的方法:MapView inside a ScrollView?
我创建了一个名为 myMapView 的类。这是它的代码:

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
没发现问题出在哪里,好像一切正​​常。如果有人可以帮助我,我将不胜感激
谢谢

4

1 回答 1

3

这就是您收到 ClassCastException 的原因。在您声明自定义地图视图的 XML 文件中,您必须实际声明自定义地图视图的名称,因此在您的情况下它将是 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"
/>
于 2012-10-07T07:56:57.910 回答