2012-02-12 102 views
2

我試圖讓默認縮放控件顯示在谷歌mapView上。 我正在查看this的代碼。但getZoomControls()已棄用。mapView不推薦使用的方法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?

回答

4

這應該足以實現你想要的。

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

那麼你不需要明確將您ZoomControls在XML,也不引用它在Java代碼中

+0

這給了我18行NullPointerException異常,這是mapView.setBuiltInZoomControls(真) ; 我會用詳細信息編輯問題 – Ephemeros 2012-02-12 16:15:24

+0

嘗試重新調整findViewById(R.layout.maptabview);在您的XML與findViewById(R.layout.mapview); (在XML中設置的實際ID) – znat 2012-02-12 16:30:43

+0

'無法解析或不是字段'我認爲R.layout存儲了佈局文件引用(即主要和任何附加),而不是它們中的元素? – Ephemeros 2012-02-12 16:53:49

相關問題