0

I did everything by following a tutorial, the map gets displayed in the tutorial but it doesn't get displayed in mine even when I did the exact same thing.

MainActivity.Java

package com.example.ankit.mrestro;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int GPS_ERRORDIALOGUE_REQUEST = 9001;
    GoogleMap mMap;
    MapView mMapView;
    Button bLogout;
    EditText etFName, etLName, etAge, etEmail, etUserName;
    UserLocalStorage userLocalStorage;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (servicesOK()){
            Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show();
            setContentView(R.layout.activity_mapview);

            mMapView = (MapView) findViewById(R.id.map);
            mMapView.onCreate(savedInstanceState);


        }
        setContentView(R.layout.activity_main);

        etFName = (EditText) findViewById(R.id.etFName);
        etLName = (EditText) findViewById(R.id.etLName);
        etAge = (EditText) findViewById(R.id.etAge);
        etEmail = (EditText) findViewById(R.id.etEmail);
        etUserName = (EditText) findViewById(R.id.etUserName);

        bLogout = (Button) findViewById(R.id.bLogout);
        bLogout.setOnClickListener(this);
        userLocalStorage = new UserLocalStorage(this);


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        if (authenticate() == true) {
            DisplaysUserDetails();
        }

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.ankit.mrestro/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    private boolean authenticate() {
        return userLocalStorage.getUserLoggedIn();

    }

    private void DisplaysUserDetails() {
        User user = userLocalStorage.GetLoggedInUser();
        etUserName.setText(user.UserName);
        etFName.setText(user.FirstName);
        etLName.setText(user.LastName);
        etAge.setText(user.Age + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bLogout:
                userLocalStorage.ClearUserData();
                userLocalStorage.SetUserLoggedIn(false);
                startActivity(new Intent(this, Login.class));
                break;
        }
    }

    public boolean servicesOK() {
        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (isAvailable == ConnectionResult.SUCCESS) {
            return true;
        } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOGUE_REQUEST);
            dialog.show();
        }
        else {
            Toast.makeText(this, "Can't connect to google play services", Toast.LENGTH_SHORT).show();

        }
        return false;


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();

    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);

    }



    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction2 = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.ankit.mrestro/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction2);
        client.disconnect();
    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.ankit.mrestro">

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="23"/>

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

        <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.gsg.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
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:icon, android:theme, android:label">

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




        <activity
            android:name=".Login"
            android:label="@string/title_activity_login"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

             <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Register"
            android:label="@string/title_activity_register"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Main2Activity"
            android:label="@string/title_activity_main2"
            android:theme="@style/AppTheme.NoActionBar"></activity>
        <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

activity_mapview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>

I've noticed that I've not created a separate button for displaying the map. Could this be the problem? If so, how do I assign a button to Google maps? I know how to create a button and assign an activity to it but how do I do that for Maps? Please help.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
Bikal Nepal
  • 477
  • 1
  • 10
  • 26
  • Possible duplicate of [Android: Google Maps not displaying](http://stackoverflow.com/questions/16551458/android-google-maps-not-displaying) – Vamsi Abbineni Jul 30 '16 at 04:43

1 Answers1

0

Where are you getting your map? You should implement OnMapReadyCallback in your Activity and then in onCreate do the following:

        if (/* play services are OK */) {
        mMapView.onCreate(savedInstanceState);
        mMapView.getMapAsync(this);
    }

And then override an onMapReady call (something like that):

    @Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
    UiSettings uiSettings = mGoogleMap.getUiSettings();
    uiSettings.setZoomControlsEnabled(true);

    if (mCameraPosition != null) {
        mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition));
    }
    addMarkers();
}

You can check out my code: https://github.com/dmytroKarataiev/EarthquakeSurvival/blob/master/app/src/main/java/com/adkdevelopment/earthquakesurvival/MapviewFragment.java

Also very important: NEVER show anyone your API key. It's against the Google rules and also not safe for you.

Dmytro Karataiev
  • 1,214
  • 1
  • 14
  • 19