-1

我正在尝试在我的 android 项目中使用 google maps API V2,问题是当我使用从布局中获得的地图时,我得到 NullPointerException。我花了几个小时在互联网上寻找,但我找不到任何东西。我很抱歉,如果它已经被问过。

这是包含片段的布局:

<?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.MapFragment" />

这是我的主要活动:

public class MainActivity extends Activity {

private GoogleMap map;

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


    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

    Log.d("MAP",map.toString());}

“toString”是为了减少问题。我知道没有任何意义,但它应该工作。这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testgooglemapsv2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.CAMERA" />

<permission
    android:name="com.example.testgooglemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.testgooglemapsv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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-permission android:name="android.permission.DISABLE_KEYGUARD" />

<application
    android:allowBackup="true"
    android:configChanges="orientation|keyboardHidden"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-library android:name="com.google.android.maps" />

    <activity
        android:name="com.example.testgooglemapsv2.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>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyB5tu4v6-jG1eiCKoSxEm_GfSZzAZt73k8" />

    <activity
        android:name="com.example.testgooglemapsv2.CameraActivity"
        android:label="@string/title_activity_camera" >
    </activity>
</application>

</manifest>

这是日志文件:

> 04-11 23:31:03.715: E/AndroidRuntime(4209): FATAL EXCEPTION: main
> 04-11 23:31:03.715: E/AndroidRuntime(4209):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.testgooglemapsv2/com.example.testgooglemapsv2.MainActivity}:
> java.lang.NullPointerException 04-11 23:31:03.715:
> E/AndroidRuntime(4209):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
> 04-11 23:31:03.715: E/AndroidRuntime(4209):   at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
> 04-11 23:31:03.715: E/AndroidRuntime(4209):   at
> android.app.ActivityThread.access$600(ActivityThread.java:141) 04-11
> 23:31:03.715: E/AndroidRuntime(4209):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
> 04-11 23:31:03.715: E/AndroidRuntime(4209):   at
> android.os.Handler.dispatchMessage(Handler.java:99) 04-11
> 23:31:03.715: E/AndroidRuntime(4209):     at
> android.os.Looper.loop(Looper.java:137) 04-11 23:31:03.715:
> E/AndroidRuntime(4209):   at
> android.app.ActivityThread.main(ActivityThread.java:5041) 04-11
> 23:31:03.715: E/AndroidRuntime(4209):     at
> java.lang.reflect.Method.invokeNative(Native Method) 04-11
> 23:31:03.715: E/AndroidRuntime(4209):     at
> java.lang.reflect.Method.invoke(Method.java:511) 04-11 23:31:03.715:
> E/AndroidRuntime(4209):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
> 04-11 23:31:03.715: E/AndroidRuntime(4209):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-11
> 23:31:03.715: E/AndroidRuntime(4209):     at
> dalvik.system.NativeStart.main(Native Method) 04-11 23:31:03.715:
> E/AndroidRuntime(4209): Caused by: java.lang.NullPointerException
> 04-11 23:31:03.715: E/AndroidRuntime(4209):   at
> com.example.testgooglemapsv2.MainActivity.onCreate(MainActivity.java:99)
> 04-11 23:31:03.715: E/AndroidRuntime(4209):   at
> android.app.Activity.performCreate(Activity.java:5104) 04-11
> 23:31:03.715: E/AndroidRuntime(4209):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
> 04-11 23:31:03.715: E/AndroidRuntime(4209):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
> 04-11 23:31:03.715: E/AndroidRuntime(4209):   ... 11 more 04-11
> 23:31:03.724: D/dalvikvm(4209): GC_CONCURRENT freed 206K, 12% free
> 2625K/2956K, paused 70ms+76ms, total 190ms 04-11 23:36:03.855:
> I/Process(4209): Sending signal. PID: 4209 SIG: 9

再次抱歉,如果这似乎是一个轻松的问题,但相信我,我已经尝试过了。谢谢你。

4

1 回答 1

-3

我找到了解决我的问题的方法。这是因为我想在地图准备好之前拿到地图。所以我像这样修改了我的代码:

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

    F = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    map = F.getMap();
    while(map==null){

    }
    if(map!=null)   
        Log.d("MAP",map.toString());
      }

它工作得很好:)

于 2013-04-12T21:07:56.337 回答