-1

I am trying to work with google maps API V2 in my android project, the problem is when I use the map that I got from the layout, I get NullPointerException. I spent hours looking on the internet but I could't find anything. I'm sorry if it has been already asked.

Here's the layout containing the fragment:

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

Here's my MainActivity:

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());}

The "toString" thing is to reduce the problem. Doesn't make any sense I know, but it should work. Here's the Manifest file :

<?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>

Here's the log file:

> 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

Sorry again if it seems to be an effortless question, but believe me, I've tried. Thank you.

Pivoine
  • 29
  • 6

1 Answers1

-3

I found the solution to my problem. It's because I'm trying to get the map, before it's ready. So I modified my code like so :

    @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());
      }

And it works perfectly fine :)

Pivoine
  • 29
  • 6
  • 2
    THIS is the accepted answer? it doesnt solve the problem and its one of the worst hacks you could imagine inside an ui-thread. – dy_ Jun 17 '13 at 13:19