0

我想在 Android 应用程序中显示谷歌地图,但地图没有显示。它生成运行时异常。我成功添加了 google-play-services_lib 库。

我正在使用以下代码:

活动.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

我的主要活动代码是

    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;
}
4

2 回答 2

0

您必须编辑声明的 XML 片段,如下所示:

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>

并替换:

mMap= ((MapFragment)  getSupportFragmentManager().findFragmentById(R.id.map))
               .getMap();

通过以下一项:

mMap= ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map))
               .getMap();

顺便说一句,请确保您正在扩展 aFragmentActivity而不是Activity.

public class MainActivity extends FragmentActivity
于 2013-07-01T10:41:46.987 回答
0

尝试这个,

public class MainActivity extends android.support.v4.app.FragmentActivity {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    mMap = ((SupportMapFragment) getSupportFragmentManager().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"));
}
}
于 2013-07-01T10:37:09.337 回答