我想在Android上設置谷歌地圖,所以我已經設置了所有東西,但是當我嘗試獲取包含地圖的片段時,錯誤日誌顯示NullPointerException
錯誤。該錯誤是由於它無法識別地圖在Android設置谷歌地圖獲取NullPointerException
我的XML佈局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.unchainedappli.SpotFragement">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
的onCreate
方法
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a handle to the Map Fragment
GoogleMap map = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
錯誤日誌:
0-17 08:12:32.884 2373-2373/com.example.user.unchainedappli E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.unchainedappli, PID: 2373
java.lang.NullPointerException
at com.example.unchainedappli.SpotFragement.onCreate(SpotFragement.java:69)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
您還沒有提到谷歌地圖V2 xml佈局 – Harshad07 2014-10-17 08:38:12
的API密鑰,但不是必須在AndroidManifiest中。 – Karimx 2014-10-17 08:40:31
我說過關於AndroidMainfest.xml文件。你確定你已經把你的API密碼 – Harshad07 2014-10-17 08:42:12