Im在GoogleMap中存在一個煩人的問題。我測試過了,地圖顯示正常。但是現在我想設置「我的位置已啓用」,以便地圖自動啓動在電話所在的位置。現在每次運行應用程序都會崩潰。我注意到,當我使用語法GoogleMap時,應用程序崩潰。 我試圖找出這個問題,並沒有結果環顧四周。使用GoogleMap V2時Android應用程序崩潰
這裏是我的Maps.java文件:
package test.test;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class Maps extends FragmentActivity implements LocationListener{
GoogleMap googlemap;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googlemap = mf.getMap();
googlemap.setMyLocationEnabled(true);
googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
Maps.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
最後我androidmanifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="test.test.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="test.test.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<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
....
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my key is here" />
</application>
以下是錯誤日誌:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.test/test.test.Maps}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at test.test.Maps.onCreate(Maps.java:23)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
這個問題是非常相似,HTTP:/ /stackoverflow.com/questions/14719602/googlemap-setmaptypegooglemap-map-type-satellite-doesnt-work – petey 2013-02-26 20:34:53
是的,但他可以在他的設備上運行它,它似乎工作。我的手機仍然有相同的問題。 – MrCaps 2013-02-26 22:19:28