2011-10-23 90 views
0

我一直在試圖創造什麼我想象會是一個相當簡單的應用程序 - 在開始活動,有一個按鈕按下,然後將推出一個MapActivity顯示您當前的位置。無法啓動新MapActivity

但是,每當我按下按鈕,我立即收到一條錯誤和我被迫退出應用程序。

這裏的日誌:

10-23 19:15:10.826: D/PhoneWindow(3227): DebugMonitor class=com.android.find.my.friends.MainActivity focus=true 
10-23 19:15:19.496: D/AndroidRuntime(3227): Shutting down VM 
10-23 19:15:19.496: W/dalvikvm(3227): threadid=3: thread exiting with uncaught exception (group=0x4001db88) 
10-23 19:15:19.496: E/AndroidRuntime(3227): Uncaught handler: thread main exiting due to uncaught exception 
10-23 19:15:19.506: E/AndroidRuntime(3227): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.find.my.friends/com.android.find.my.friends.ShowMap}: java.lang.NullPointerException 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2486) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at android.app.ActivityThread.access$2100(ActivityThread.java:123) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1843) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at android.os.Looper.loop(Looper.java:123) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at android.app.ActivityThread.main(ActivityThread.java:4321) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at java.lang.reflect.Method.invoke(Method.java:521) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at dalvik.system.NativeStart.main(Native Method) 
10-23 19:15:19.506: E/AndroidRuntime(3227): Caused by: java.lang.NullPointerException 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at com.android.find.my.friends.ShowMap.onCreate(ShowMap.java:30) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2418) 
10-23 19:15:19.506: E/AndroidRuntime(3227):  ... 11 more 
10-23 19:15:19.516: I/dalvikvm(3227): threadid=7: reacting to signal 3 
10-23 19:15:19.546: I/dalvikvm(3227): Wrote stack trace to '/data/anr/traces.txt' 

我上的HTC紋身測試這個運行Android 1.6。我以前也能夠創建一個只顯示MapActivity的應用程序。

任何幫助傢伙?

[編輯]

這裏是我的onCreate方法:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); // bind the layout to the activity 

    mapView = (MapView) findViewById(R.id.mapview); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setStreetView(true); 
    mapController = mapView.getController(); 
    mapController.setZoom(14); // Zoom 1 is world view 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
      0, new GeoUpdateHandler()); 

} 
+0

放一個斷點到ShowMap.onCreate在第30行,你有NullPointerException異常那裏。 – slkorolev

回答

1

看來,你在你的ShowMap類有NullPointerException在第30行。如果你可以發佈你的onCreate方法和第30行,我可以幫你找出原因。

難道你還粘貼異常本身發生就行了? findViewById可能返回null,在這種情況下,您的mapView將爲空。確保您的MapView的佈局佈局與您傳入setContentView的佈局相同。

+0

我非常簡單,只是複製從這裏的代碼,所以也許你會得到更好的畫面,如果我只是發送了鏈接:http://www.vogella.de/articles/AndroidLocationAPI/article.html#googlemaps (看「Google地圖」部分) – shamsad97

+1

但是您有一個主要活動,您可以按下一個按鈕,將您帶到地圖活動的正確位置?我認爲這個問題可能是你在呼喚'的setContentView(R.layout.main)'在地圖中活動時要調用'的setContentView(R.layout.map)',或任何你與地圖叫你的xml文件佈局。 – Craigy

+0

謝謝!是的,這是正確的,大規模noob錯誤,感謝指出:) – shamsad97