0

I've been looking for an entire week and I can't seem to find any solution so here I am subscribed :)

I'm trying to display a simple map to start with but when I launch my application there is only a blank activity (well a bit darker than white) with Google written on the bottom-left.

My emulator runs perfectly well Google Maps on other application so I thought it was maybe a problem of API Key but I just checked and it was ok.

Tell me if there is something wrong, my Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.slimane.pinpoint">
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <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" />
    <!-- The following two permissions are not required to use
         Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <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" />

    <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.gms.version"
            android:value="@integer/google_play_services_version" />


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


        <activity
            android:name=".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>
        <activity
            android:name=".MainActivity3"
            android:label="@string/title_activity_main_activity2"></activity>
        <activity
            android:name=".MenuOptions"
            android:label="@string/title_activity_menu_options"></activity>
        <activity
            android:name=".MenuAmis"
            android:label="@string/title_activity_menu_amis"></activity>
        <activity
            android:name=".MapEditor_StepOne"
            android:label="@string/title_activity_map_editor__step_one"></activity>
        <activity
            android:name=".MenuEvents"
            android:label="@string/title_activity_menu_events"></activity>

        <activity android:name=".MapsActivity"
            android:label=".maps"></activity>
    </application>

</manifest>

My map activity:

package com.example.slimane.pinpoint;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;

import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.SupportMapFragment;

import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;


public class MapEditor_StepOne extends FragmentActivity {
    private GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_editor__step_one);








    }


}

My related layout:

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

</LinearLayout>

I'm starting to losing my hair..

Thanks

1 Answers1

0

I think you haven't set up the Google Map there is nothing in your MapAcitivity.

Sample code:

public class MainActivity extends Activity {

    private static LatLng goodLatLng = new LatLng(37, -120);
    private GoogleMap googleMap;
    private EditText et_address, et_finalAddress;
    LatLng addressPos, finalAddressPos;
    Marker addressMarker;

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

        et_address = (EditText) findViewById(R.id.addressEditText);
        et_finalAddress = (EditText) findViewById(R.id.finalAddressEditText);


        // Initial Map
        try {

            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        // Put a dot on my current location
        googleMap.setMyLocationEnabled(true);
        googleMap.setIndoorEnabled(true);
        googleMap.setTrafficEnabled(true);
        // 3D building
        googleMap.setBuildingsEnabled(true);
        // Get zoom button
        googleMap.getUiSettings().setZoomControlsEnabled(true);

        Marker marker = googleMap.addMarker(new MarkerOptions()
                .position(goodLatLng)
                .title("Hello"));
    }

For more details, please refer to my github here.

Also, please try to follow this step by step, you will finally get a map on your phone.

bjiang
  • 6,068
  • 2
  • 22
  • 35
  • Ok thanks a lot, I tried to use .getMap (without exception though) and similarly, the application will shutdown. How can I get what comes out of the exception? Meanwhile I'll try to follow your step by step tutorial. – AIT SI ALI Slimane Feb 19 '15 at 21:10
  • For the `exception`, you need to refer to your `logcat`. For how to get it, you can refer to [here](http://stackoverflow.com/a/23353174/4186942). – bjiang Feb 19 '15 at 21:41
  • Ok! I didn't know that was the logcat but now I know how to get there thanks. We're getting closer and problem is what I suspected: there comes out a NullPointerException from the following line: `code`googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); `code` So, at this point I really don't know what to do..I've followed all your steps and my emulator works perfectly fine, it's not an API key problem either..I'm sure it must be something really dumb but I can't find because I've been for too long on the problem I guess.. – AIT SI ALI Slimane Feb 19 '15 at 22:56
  • Which following line? – bjiang Feb 19 '15 at 22:58
  • You can try [this](http://stackoverflow.com/questions/16591750/mapfragment-nullpointerexception-google-maps-v2-android) and [this](http://stackoverflow.com/questions/16967328/mapfragment-findfragmentbyid-always-null). Also you can download the whole project from my [Github](https://github.com/jbj88817/GoogleMapExample-android), and run it in `Android Studio`. – bjiang Feb 19 '15 at 23:02
  • SupportFragment doesn't work because it's api>12. I'll try to run your code tomorrow. – AIT SI ALI Slimane Feb 19 '15 at 23:24
  • Ok your project works perfectly well but when I put your activity (and layout) in my projet the same error is displayed.. Maybe it's an API key problem after all, i'll try to launch your project with my API key. – AIT SI ALI Slimane Feb 21 '15 at 19:01
  • The `NullPointerException`? If you can share your project on github or dropbox, I can take a look at it, and try to help you out. Yeah, maybe your API key also has problem. – bjiang Feb 21 '15 at 19:03
  • Problem solved. Well, not really, I restarted the whole project and it works perfectly, still dunno why and it bothers me a little but eh, let's move on. Thanks for the help! – AIT SI ALI Slimane Mar 03 '15 at 19:06