0

I'm Specifically building a little Map application with three activity windows. One is for the menu, the other one it's the map and the last one is about my enterprise.

The problem is that i'm working with radio buttons and this radio buttons the only thing that they are going to do is to give values according with the radio button they choose (I haven't put the code that makes that people can only choose one radio button, if you can help me with that too I will be very grateful) but when i tried to receive the value in the maps activity to use it into a if statement which is going to determine what markers are going to be written.

I can't, because it's say that im referencing an null object. I don't know what i'm doing wrong.

MainActivity.java

    package com.example.erik.oportour;

    import android.app.Activity;``
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RadioButton;

    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;

import java.io.Serializable;

@SuppressWarnings("serial")
public class MainActivity extends Activity implements Serializable{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioButton Turismo = (RadioButton) findViewById(R.id.Turismo);
        RadioButton Escuelas = (RadioButton) findViewById(R.id.Escuelas);
        RadioButton Baños = (RadioButton) findViewById(R.id.Baños);
        Button button = (Button) findViewById(R.id.start);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent i = new Intent(MainActivity.this, MapsActivity.class);

                Intent explicitit_intent;
                explicitit_intent = new Intent(MainActivity.this, MapsActivity.class);
                int a = 0;
                switch (v.getId()) {

                    case R.id.Turismo:
                        a = 1;
                        explicitit_intent.putExtra("a", a);
                        break;
                    case R.id.Escuelas:
                        a = 2;
                      explicitit_intent.putExtra("a", a);
                        break;
                    case R.id.Baños:
                       a = 3;
                        explicitit_intent.putExtra("a", a);
                        break;
                }
                ;
                startActivity(explicitit_intent);
            }
        });
    }

    public void lanzar(View view){
        Intent acerca = new Intent(MainActivity.this, AcercaDe.class);
        startActivity(acerca);
    }
}

And here it's my MapsActivity

package com.example.erik.oportour;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.Serializable;
import java.util.Map;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, Serializable,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {
    private GoogleMap mMap;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    Marker mCurrLocationMarker;
    LocationRequest mLocationRequest;
    int b;
    int a;

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

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkLocationPermission();
        }
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


        Intent explicitit_intent = this.getIntent();
        //if(explicitit_intent !=null)
            b = explicitit_intent.getIntExtra("a", a);
    }





    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        //Initialize Google Play Services
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    android.Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
                mMap.setMyLocationEnabled(true);
            }
        } else {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        }
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        if (ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }

        //Place current location marker
        LatLng Ubi = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(Ubi);
        markerOptions.title("Ahora mismo se encuentra aquí");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mCurrLocationMarker = mMap.addMarker(markerOptions);

        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLng(Ubi));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        Marca();

        //stop location updates
       if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }

    }
        public void Marca() {
            if (b==1) {
                mMap.clear();
                LatLng Tec = new LatLng(28.688684, -100.571503);
                MarkerOptions markertec = new MarkerOptions();
                markertec.position(Tec);
                markertec.title("Instituto Tecnológico de Piedras Negras");
                markertec.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markertec);

                LatLng CBTIS = new LatLng(28.685598, -100.559615);
                MarkerOptions markercbtis = new MarkerOptions();
                markercbtis.position(CBTIS);
                markercbtis.title("CBTIS #34");
                markercbtis.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markercbtis);

                LatLng UANE = new LatLng(28.6789875, -100.5467568);
                MarkerOptions markeruane = new MarkerOptions();
                markeruane.position(UANE);
                markeruane.title("Universidad Autonoma del Noreste (UANE)");
                markeruane.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markeruane);

                LatLng UADEC = new LatLng(25.4791935, -100.9923523);
                MarkerOptions markeruadec = new MarkerOptions();
                markeruadec.position(UADEC);
                markeruadec.title("UAdeC Facultad De Ciencias de la Administración");
                markeruadec.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markeruadec);

                LatLng Viz = new LatLng(28.6853947, -100.5606025);
                MarkerOptions markerviz = new MarkerOptions();
                markerviz.position(Viz);
                markerviz.title("Universidad Vizcaya De Las Américas Piedras Negras");
                markerviz.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markerviz);

                LatLng Donb = new LatLng(28.6913053, -100.5575126);
                MarkerOptions markerdb = new MarkerOptions();
                markerdb.position(Donb);
                markerdb.title("Instituto Don Bosco");
                markerdb.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markerdb);

                LatLng JP = new LatLng(28.6910418, -100.5562895);
                MarkerOptions markerjp = new MarkerOptions();
                markerjp.position(JP);
                markerjp.title("Instituto Jean Piaget");
                markerjp.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markerjp);

                LatLng CZ = new LatLng(28.6829398, -100.5772642);
                MarkerOptions markercz = new MarkerOptions();
                markercz.position(CZ);
                markercz.title("Escuela Primaria Coahuila De Zaragoza T.M.");
                markercz.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markercz);

                LatLng JGG = new LatLng(28.6829398, -100.5772642);
                MarkerOptions markerjgg = new MarkerOptions();
                markerjgg.position(JGG);
                markerjgg.title("Escuela Primaria José Guadalupe González");
                markerjgg.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markerjgg);
            }
                else if (b==2) {
                mMap.clear();
                LatLng PDC = new LatLng(28.6948234, -100.5251208);
                MarkerOptions markerpdc = new MarkerOptions();
                markerpdc.position(PDC);
                markerpdc.title("Plaza de las Culturas");
                markerpdc.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markerpdc);

                LatLng MP = new LatLng(28.7049726, -100.5384911);
                MarkerOptions markermp = new MarkerOptions();
                markermp.position(MP);
                markermp.title("Macroplaza 1 y 2");
                markermp.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markermp);

                LatLng MU = new LatLng(28.7060651, -100.5166257);
                MarkerOptions markermu = new MarkerOptions();
                markermu.position(MU);
                markermu.title("Museo de la frontera norte");
                markermu.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markermu);


                LatLng PDR = new LatLng(28.68843, -100.5745573);
                MarkerOptions markerpdr = new MarkerOptions();
                markerpdr.position(PDR);
                markerpdr.title("Paseo del Rio");
                markerpdr.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markerpdr);

                LatLng TJM = new LatLng(28.7037675, -100.5143996);
                MarkerOptions markerTJM = new MarkerOptions();
                markerpdr.position(TJM);
                markerpdr.title("Teatro Jose Manuel Maldonado");
                markerpdr.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markerTJM);

                LatLng TPN = new LatLng(28.68843, -100.5745573);
                MarkerOptions markerTPN = new MarkerOptions();
                markerTPN.position(TPN);
                markerTPN.title("Teatro Piedras Negras");
                markerTPN.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                mCurrLocationMarker = mMap.addMarker(markerTPN);
            }
        }


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

    public boolean checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            // Asking user if explanation is needed
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    android.Manifest.permission.ACCESS_FINE_LOCATION)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

                //Prompt the user once explanation has been shown
                ActivityCompat.requestPermissions(this,
                        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);


            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
            }
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted. Do the
                    // contacts-related task you need to do.
                    if (ContextCompat.checkSelfPermission(this,
                            android.Manifest.permission.ACCESS_FINE_LOCATION)
                            == PackageManager.PERMISSION_GRANTED) {

                        if (mGoogleApiClient == null) {
                            buildGoogleApiClient();
                        }
                        mMap.setMyLocationEnabled(true);
                    }

                } else {

                    // Permission denied, Disable the functionality that depends on this permission.
                    Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
                }
                return;
            }

            // other 'case' lines to check for other permissions this app might request.
            // You can add here other case statements according to your requirement.
        }
    }


}

My manifest

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyBzwz5W2be2sxBuspq2XDitTV1i8PuWS5s" />

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

</manifest>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Show the logcat and stack trace – Diego Torres Milano Apr 27 '17 at 03:28
  • Possible duplicate: http://stackoverflow.com/q/41777482/5885018 – statosdotcom Apr 27 '17 at 03:28
  • A Null reference? Also duplicate. http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – OneCricketeer Apr 27 '17 at 03:34
  • Suggestion: We don't need to see more than two or three markers for your question. Show a [mcve], please – OneCricketeer Apr 27 '17 at 03:35
  • Get the answer in the question that @cricket_007 has comment. Thank you, im having another problem now... This one Logcat: 04-27 12:58:39.281 18122-20442/com.example.erik.oportour W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found. 04-27 12:58:39.301 18122-20442/com.example.erik.oportour I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:2 – Erik Ramon Mtz Apr 27 '17 at 04:06
  • Those are warnings and info messages, not errors – OneCricketeer Apr 27 '17 at 04:17

0 Answers0