when I trying to run this app It does not show map and display error like:
09-19 12:50:27.666: E/AndroidRuntime(525): 致命异常: main 09-19 12:50:27.666: E/AndroidRuntime(525): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.mapdemo/com.example.mapdemo.BasicMapActivity}:java.lang.NullPointerException 09-19 12:50:27.666:E/AndroidRuntime(525):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 09 -19 12:50:27.666: E/AndroidRuntime(525): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 09-19 12:50:27.666: E/AndroidRuntime(525): 在 android.app .ActivityThread.access$1500(ActivityThread.java:117) 09-19 12:50:27.666: E/AndroidRuntime(525): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 09-19 12: 50:27.666: E/AndroidRuntime(525): 在 android.os.Handler.dispatchMessage(Handler.java:99) 09-19 12:50:27.666:E/AndroidRuntime(525): 在 android.os.Looper.loop(Looper.java:123) 09-19 12:50:27.666: E/AndroidRuntime(525): 在 android.app.ActivityThread.main(ActivityThread.java :3683) 09-19 12:50:27.666: E/AndroidRuntime(525): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-19 12:50:27.666: E/AndroidRuntime(525): 在java.lang.reflect.Method.invoke(Method.java:507) 09-19 12:50:27.666: E/AndroidRuntime(525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :839) 09-19 12:50:27.666: E/AndroidRuntime(525): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-19 12:50:27.666: E/AndroidRuntime (525): 在 dalvik.system.NativeStart.main(Native Method) 09-19 12:50:27.666: E/AndroidRuntime(525): 由: java.lang.NullPointerException 09-19 12:50:27.666: E /AndroidRuntime(525):在 com.example.mapdemo。BasicMapActivity.setUpMapIfNeeded(BasicMapActivity.java:79) 09-19 12:50:27.666: E/AndroidRuntime(525): at com.example.mapdemo.BasicMapActivity.onCreate(BasicMapActivity.java:44) 09-19 12:50: 27.666: E/AndroidRuntime(525): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-19 12:50:27.666: E/AndroidRuntime(525): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread .java:1611) 09-19 12:50:27.666: E/AndroidRuntime(525): ... 11 更多performLaunchActivity(ActivityThread.java:1611) 09-19 12:50:27.666: E/AndroidRuntime(525): ... 11 更多performLaunchActivity(ActivityThread.java:1611) 09-19 12:50:27.666: E/AndroidRuntime(525): ... 11 更多
package com.example.mapdemo;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class BasicMapActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
My xml code is
==============
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
My manifest code is:
====================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapdemo"
android:versionCode="1"
android:versionName="1.0">
<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/demo_title"
android:hardwareAccelerated="true">
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyD5Tz4DguP8xOX2Z2WUq2Xa54qqN3eXx_Y"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".BasicMapActivity"
android:label="@string/basic_map"/>
<activity
</application>
</manifest>