mapView不推荐使用方法getZoomControls?

时间:2012-02-12 15:54:16

标签: java android nullpointerexception zoom android-mapview

我正在尝试将默认缩放控件显示在google mapView上。 我在查看this代码。但是不推荐使用getZoomControls()。

我正在尝试使用setBuiltInZoomControls,但是我遇到了错误;

当我调试时,异常是;

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.maptab2/com.test.maptab2.MapTabView}: java.lang.NullPointerException


02-05 09:49:21.239: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-05 09:49:22.616: E/AndroidRuntime(357): FATAL EXCEPTION: main
02-05 09:49:22.616: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class <unknown>
02-05 09:49:22.616: E/AndroidRuntime(357):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-05 09:49:22.616: E/AndroidRuntime(357):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-05 09:49:22.616: E/AndroidRuntime(357):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-05 09:49:22.616: E/AndroidRuntime(357):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-05 09:49:22.616: E/AndroidRuntime(357):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 09:49:22.616: E/AndroidRuntime(357):  at android.os.Looper.loop(Looper.java:123)
02-05 09:49:22.616: E/AndroidRuntime(357):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-05 09:49:22.616: E/AndroidRuntime(357):  at java.lang.reflect.Method.invokeNative(Native Method)
02-05 09:49:22.616: E/AndroidRuntime(357):  at java.lang.reflect.Method.invoke(Method.java:521)
02-05 09:49:22.616: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-05 09:49:22.616: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-05 09:49:22.616: E/AndroidRuntime(357):  at dalvik.system.NativeStart.main(Native Method)
02-05 09:49:22.616: E/AndroidRuntime(357): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mapintab/com.test.mapintab.MapInTabActivit

第18行是mapView.setBuiltInZoomControls(true);

我的地图正在一个标签中显示,我在我的主要课程中这样做(它处理标签 - 每个都是他们自己的课程的内容);

Intent i = new Intent(this,MapTabView.class);

TabHost.TabSpec spec;

spec = getTabHost().newTabSpec("tab1");
spec.setContent(i);
spec.setIndicator("Map");
getTabHost().addTab(spec);

这是我的代码:

MapTabView.java

package com.test.maptab2;

import android.os.Bundle;
import android.view.View;
import android.widget.ZoomControls;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MapTabView extends MapActivity {

    MapView mapView;

    @Override
    public void onCreate (Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.maptabview);

        mapView = (MapView) findViewById(R.layout.maptabview);
            mapView.setBuiltInZoomControls(true);

    }

    @Override
    public boolean isRouteDisplayed(){
        return false;
    }

}

maptabview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/maptablayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- Actual map -->
    <com.google.android.maps.MapView android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="0HRMcD5o6WrBVhmwbWpeyeavZ67PXWOvJeeCx2g"/>

</RelativeLayout>

我刚刚意识到这和我在这里遇到的错误相同:Android unable to start activity, error inflating class?

1 个答案:

答案 0 :(得分:4)

这应该足以达到你想要的效果。

    mapView = (MapView) findViewById(R.id.historymapview);
    mapView.setBuiltInZoomControls(true);

然后您不需要将ZoomControls显式地放在XML中,也不需要在Java代码中引用它