0

I was developing a Android App using Android Studio, using Google Maps Services Location, and a get an error when a try to pass the parameters using the Parcelable interface, in my POJO (Lineas Class), based on API Android implementation.

The Apps should work show the best route using the differents lines of the Madrid train's Stations, based on your current location.

The Code of class Lineas where I found the error is this:

package com.example.rutas_metro_madrid;

import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;

import static android.content.ComponentName.readFromParcel;

public class Lineas extends Location implements Parcelable {

    private String nombre;
    Location[] estaciones;
    protected int origenRuta;
    protected int finalRuta;
    private double datosParadaOrigen;
    private double datosParadaDestino;
    private Parcel parcel;


    public void distancias(Location origen, Location destino){
        //Almacenamos la distancia a la primera estacion, desde el origen
        // y comprobamos cual es la menor.
        datosParadaOrigen = origen.distanceTo(estaciones[0]);
        datosParadaDestino = destino.distanceTo(estaciones[0]);

        for (int i =0; i<estaciones.length; i++){

            if(origen.distanceTo(estaciones[i])<datosParadaOrigen){

                origenRuta = i;
                datosParadaOrigen = origen.distanceTo(estaciones[i]);
            }
            if(destino.distanceTo(estaciones[i])<datosParadaDestino){

                finalRuta = i;
                datosParadaDestino = destino.distanceTo(estaciones[i]);
            }
        }
    }

    public double sumaDisMetro(){

        return datosParadaOrigen+datosParadaDestino;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public void setEstaciones(Location[] estaciones) {
        this.estaciones = estaciones;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<Lineas> CREATOR
            = new Parcelable.Creator<Lineas>() {
        public Lineas createFromParcel(Parcel parcel) {
            return new Lineas(parcel);
        }

        public Lineas[] newArray(int size) {
            return new Lineas[size];
        }
    };

    public Lineas(){
    }

    //Constructor de la Clase Parcelable.
    public Lineas(Parcel parcel) {
        readFromParcel(parcel);
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {

        //Almacenamos el objeto Linea en un Parcel.
        parcel.writeString(nombre);
        parcel.writeTypedArray(estaciones, 0);
        parcel.writeInt(origenRuta);
        parcel.writeInt(finalRuta);
        parcel.writeDouble(datosParadaDestino);
        parcel.writeDouble(datosParadaOrigen);
    }

    private void readFromParcel(Parcel parcel){

        //Hay que leerlo en el mismo sentido en el que lo escribimos.
        nombre = parcel.readString();
        estaciones = parcel.createTypedArray(Location.CREATOR);
        origenRuta = parcel.readInt();
        finalRuta = parcel.readInt();
        datosParadaOrigen = parcel.readDouble();
        datosParadaDestino = parcel.readDouble();
    }
}


The error which I get is this:

C:\Users\manul\AndroidStudioProjects\Rutas_metro_madrid\app\src\main\java\com\example\rutas_metro_madrid\Lineas.java:74: error: no suitable constructor found for Location(no arguments)
    public Lineas(){
                   ^
    constructor Location.Location(String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Location.Location(Location) is not applicable
      (actual and formal argument lists differ in length)

If you any idea of why happens this I would appreciate your colaboration.

Thanks for advance!

Manuel Lucas
  • 636
  • 6
  • 17

3 Answers3

0

Try converting the Location Array to generic Parcelable Array when reading or writing. You can refer to this issue for more info- Writing arrays of Parcelables to a Parcel in Android

0

The reason you get this error is because there is no default constructor in the parent Location class.

You can fix the error by removing extends Location from the class declaration. I don't see any reason why you would need Location as a parent class.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • The reason is I want to keep the geographical position of different Lines of the Madrid's Metro stations. So in order to acccess to the latitud and longuitud I need my class Lineas extends of Location. – Manuel Lucas Aug 30 '20 at 13:39
  • You are not adding extra behavior to "location" class, so this is an incorrect use of inheritance. What you need is an instance variable of "Location" type, not extend the Location class. – Joni Aug 30 '20 at 14:49
  • But if I change by an instance of Location class, I can pass all the information which I put on "Lineas" class. – Manuel Lucas Aug 30 '20 at 16:35
  • This class is which I use on Onther class called "Rutas", where it represent the different Stops which all their information, which I need to do the differents calculations for the best route based on your current location. – Manuel Lucas Aug 30 '20 at 16:42
0

Class Rutas:

package com.example.rutas_metro_madrid;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Arrays;

public class Rutas extends AppCompatActivity {

    private String linea;
    private Lineas[]paradas;

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

        //ALMACENAMOS LOS DATOS DE LA MEJOR RUTA
        Intent datosRuta = this.getIntent();
        String linea = datosRuta.getStringExtra("LINEAS");
        //ESTABLECEMOS SU NOMBRE EN EL TEXTVIEW
        ((TextView)findViewById(R.id.Linea)).setText(linea);

        Bundle extras = getIntent().getExtras();
        Parcelable [] datos = datosRuta.getParcelableArrayExtra("PARADAS");
        Location [] ruta = Arrays.copyOf(datos, datos.length, Location[].class);

        LinearLayout rutaContenedor = ((LinearLayout)findViewById(R.id.pantalla));

        LayoutInflater inflador = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);

        pintaRuta(inflador, R.drawable.esferainiciofin, ruta[0].getProvider(), rutaContenedor);

        String texto = getString(R.string.a_pie) + " " + (int) (ruta[0].distanceTo(ruta[1]));
        texto += " " + getString(R.string.metros);

        pintaRuta(inflador, R.drawable.caminando, texto, rutaContenedor);

        for (int i = 1; i<ruta.length; i++){

            pintaRuta(inflador, R.drawable.esferainiciofin, ruta[i].getProvider(), rutaContenedor);
        }

        if(ruta.length>2){
            texto = getString(R.string.a_pie) + " " + (int) (ruta[ruta.length-2].distanceTo((ruta[ruta.length-1])));

            pintaRuta(inflador, R.drawable.caminando, texto, rutaContenedor);
        }

        pintaRuta(inflador, R.drawable.esferainiciofin, ruta[ruta.length-1].getProvider(), rutaContenedor);

    }

    private void pintaRuta(LayoutInflater inflador, int imagen, String textoImagen, LinearLayout contendor){

        LinearLayout distanciaEstaciones = (LinearLayout) inflador.inflate(R.layout.distancia_estaciones, null);

        ((ImageView) distanciaEstaciones.findViewById(R.id.icono)).setImageResource(imagen);

        ((TextView) findViewById(R.id.texto)).setText(textoImagen);

        //Le pasamos a ActivityRutas, la Ruta que hemos confeccionado.
        contendor.addView(distanciaEstaciones);
    }

    public void mapa(View vista){

        Bundle miBundle = getIntent().getExtras();
        Intent intento = new Intent(this, MapsActivity.class);

        intento.putExtras(miBundle);
        startActivity(intento);

    }
    public void mejorRuta(Location origen, Location destino, Lineas[]lasLineas){

        Lineas mejorLinea = null;

        for (Lineas linea: lasLineas){

            linea.distancias(origen, destino);

            //Alamacenamos la mejor linea.
            if(mejorLinea==null || linea.sumaDisMetro() < mejorLinea.sumaDisMetro()){
                mejorLinea=linea;
            }
            //Si la distancia al destino es menor que la recorrida en metro, no establecemos ninguna linea.
            if(mejorLinea==null || origen.distanceTo(destino)<mejorLinea.sumaDisMetro()){

                linea=null;

                paradas = new Lineas[2];
                paradas[0]= (Lineas) origen;
                paradas[1]= (Lineas) destino;

                return;
            }
            //En el caso de que sea mejor coger el metro, calculamos al paradas.
            paradas = (Lineas[]) new Location[(mejorLinea.finalRuta - mejorLinea.origenRuta) + 2];

            paradas[0] = (Lineas) origen;

            paradas[paradas.length-1] = (Lineas) destino;
            //No recorremos las posiciones de origen y destino.
            for (int i=1;i<paradas.length-2;i++){
                paradas[i]= (Lineas) mejorLinea.estaciones[i+mejorLinea.origenRuta-1];
            }
        }
    }
}
Manuel Lucas
  • 636
  • 6
  • 17