Search code examples
javaandroidgoogle-mapsmain-activity

GpsLocation: Invalid method declaration; return type required


When trying to integrate googleMaps into my MainActivity following instructions here, the word GpsLocation(orange box 6 on linked page) goes red and says- Invalid method declaration; return type required.

I know this means it has to be inside of a button or a void or something, but here I have no idea what it needs to be in. So please, take a look at my MainActivity, and tell me where I am going wrong.

Here's my MainActivity:

package com.lalalaala.mapstest;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MainActivity extends AppCompatActivity {
    private GoogleMap googleMap; // Might be null if Google Play services APK is not available.

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


        if (googleMap == null) {

            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
            }
            else {
                // Changing map type
                //TODO
            }
        }
    }

    public GpsLocation (Context mContext, TextView gpsStatusTextView) {
        this.mContext = mContext;
        this.gpsStatusTextView = gpsStatusTextView;
        getLocation();
    }
}

Thank you


Solution

  • A method needs to have a return type in your case GpsLocation and a name. Currently you only have a return type.

    public GpsLocation methodName (Context mContext, TextView gpsStatusTextView) {
    
    }
    

    not

    public GpsLocation (Context mContext, TextView gpsStatusTextView) {
    
    }