我想在Android應用程序中顯示Google Map,但地圖不顯示。它會產生運行時異常。我成功添加了google-play-services_lib庫。如何在Android中顯示Google Map v2
我使用下面的代碼:
activity.xml
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
我manifest.xml文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.tekguc.umut.googlemapsmapsandroidv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<permission
android:name="com.example.androidmapview.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.androidmapview.permission.MAPS_RECEIVE" />
<uses-permission
android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"/>
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission
android:name="android.permission.INTERNET"/>
<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:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBqkAj48WktmfULPBYgoXBGhYzSACisR5Q"/>
<activity
android:name="info.tekguc.umut.googlemapsmapsandroidv2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
和我的日誌貓:
07-01 15:43:49.491: E/Trace(2439): error opening trace file: No such file or directory (2)
07-01 15:43:50.221: E/AndroidRuntime(2439): FATAL EXCEPTION: main
07-01 15:43:50.221: E/AndroidRuntime(2439): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.tekguc.umut.googlemapsmapsandroidv2/info.tekguc.umut.googlemapsmapsandroidv2.MainActivity}: java.lang.NullPointerException
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.os.Looper.loop(Looper.java:137)
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-01 15:43:50.221: E/AndroidRuntime(2439): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 15:43:50.221: E/AndroidRuntime(2439): at java.lang.reflect.Method.invoke(Method.java:511)
07-01 15:43:50.221: E/AndroidRuntime(2439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-01 15:43:50.221: E/AndroidRuntime(2439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-01 15:43:50.221: E/AndroidRuntime(2439): at dalvik.system.NativeStart.main(Native Method)
07-01 15:43:50.221: E/AndroidRuntime(2439): Caused by: java.lang.NullPointerException
07-01 15:43:50.221: E/AndroidRuntime(2439): at info.tekguc.umut.googlemapsmapsandroidv2.MainActivity.onCreate(MainActivity.java:24)
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.app.Activity.performCreate(Activity.java:5008)
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-01 15:43:50.221: E/AndroidRuntime(2439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-01 15:43:50.221: E/AndroidRuntime(2439): ... 11 more
和我mainactivity代碼
public class MainActivity extends Activity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);
Marker ciu = mMap.addMarker(new MarkerOptions()
.position(CIU).title("My Office"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
發佈您的MainActivity代碼 –