【问题标题】:mapView deprecated method getZoomControls?mapView 已弃用方法 getZoomControls?
【发布时间】:2012-03-04 05:50:24
【问题描述】:

我正在尝试让默认缩放控件显示在 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?

【问题讨论】:

    标签: java android nullpointerexception zooming android-mapview


    【解决方案1】:

    这应该足以实现您想要的。

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

    那么你不需要在 XML 中显式放置 ZoomControls,也不需要在你的 java 代码中引用它

    【讨论】:

    • 这在第 18 行给了我一个 nullPointerException,即 mapView.setBuiltInZoomControls(true);我将使用详细信息编辑问题
    • 尝试替换 findViewById(R.layout.maptabview);在您的 XML 中使用 findViewById(R.layout.mapview); (在您的 XML 中设置的实际 id)
    • '无法解析或不是字段' 我认为 R.layout 存储了布局文件引用(即主要和任何附加),而不是其中的元素?
    猜你喜欢
    • 2016-06-08
    • 2012-09-20
    • 2020-11-28
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多